-
Notifications
You must be signed in to change notification settings - Fork 77
/
student_add.py
98 lines (82 loc) · 2.85 KB
/
student_add.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''This file adds the data (student images) to weaviate instance
in the appropriate format.'''
import pickle
import weaviate
import uuid
import datetime
import base64, json, os
from student_test import getFaces
def generate_uuid(class_name: str, identifier: str,
test: str = 'teststrong') -> str:
""" Generate a uuid based on an identifier
:param identifier: characters used to generate the uuid
:type identifier: str, required
:param class_name: classname of the object to create a uuid for
:type class_name: str, required
"""
test = 'overwritten'
return str(uuid.uuid5(uuid.NAMESPACE_DNS, class_name + identifier))
# Connect to weaviate instance
client = weaviate.Client("http://localhost:8080")
print("Client created (student_add.py file)")
#Checking if caption schema already exists, then delete it
current_schemas = client.schema.get()['classes']
for schema in current_schemas:
if schema['class']=='Students':
client.schema.delete_class('Students')
# Create a schema to add Students
class_obj = {
"class": "Students",
"description": "Each example is an image of a student of DSAI discipline.",
"moduleConfig": {
"img2vec-neural": {
"imageFields": [
"image"
]
}
},
"properties": [
{
"dataType": [
"blob"
],
"description": "Coloured image",
"name": "image"
},
{
"dataType": [
"number"
],
"description": "Label number for the given image.",
"name": "labelNumber"
},
{
"dataType": [
"string"
],
"description": "label name (student name) of the given image.",
"name": "labelName"
}
],
"vectorIndexType": "hnsw",
"vectorizer": "img2vec-neural"
}
client.schema.create_class(class_obj)
print("Schema class created")
# You can include more labels from the train folder, I have used 3 of them.
folders = os.listdir("students/")
for fol in folders:
for img in os.listdir(f"students/{fol}"):
try:
# Adding base64 encoded image using weaviate utility function
encoded_image = weaviate.util.image_encoder_b64(f"students/{fol}/{img}")
data_properties = {
"labelName": fol,
"image": encoded_image
}
client.data_object.create(data_properties, "Students", generate_uuid('Students',fol+img))
except:
continue
path = "students/"+fol
print(f"{len(os.listdir(path))} Student images from {fol} added.")
print("Images added")