forked from weaviate/weaviate-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent_own_add.py
89 lines (73 loc) · 2.63 KB
/
student_own_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
'''This file adds the data (vegetable images) to weaviate instance
in the appropriate format.'''
import pickle
import weaviate
import uuid
import datetime
import base64, json, os
from own_vec import getFaceEncoding
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_own_add.py file)")
client.schema.delete_all()
# Create a schema to add Students
class_obj = {
"class": "Students",
"description": "Each example is an image of a student of DSAI discipline.",
"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"
}
],
"vectorizer": "none"
}
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
}
# using my own encoding generated using OpenCV
vec = getFaceEncoding(f"students/{fol}/{img}")
client.data_object.create(data_properties, "Students", generate_uuid('Students',fol+img),vector=vec)
except:
continue
path = "students/"+fol
print(f"{len(os.listdir(path))} Student images from {fol} added.")
print("Images added")