-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathveg_add.py
100 lines (87 loc) · 3.01 KB
/
veg_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
99
100
'''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
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))
def log(i: str) -> str:
""" A simple logger
:param i: the log message
:type i: str
"""
now = datetime.datetime.utcnow()
print(now, "| " + str(i))
client = weaviate.Client("http://localhost:8080")
print("Client created")
#Checking if caption schema already exists, then delete it
current_schemas = client.schema.get()['classes']
for schema in current_schemas:
if schema['class']=='Vegetables':
client.schema.delete_class('Vegetables')
# Create a schema to add vegetables
class_obj = {
"class": "Vegetables",
"description": "Each example is an image of a vegetable, associated with a label from available classes.",
"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 (description) 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 = ['Cauliflower','Carrot','Radish']
for fol in folders:
# I took only 100 images from each folder 'Cauliflower','Carrot' and 'Radish'.
cnt = 100
for img in os.listdir(f"/train/{fol}"):
# Make sure to change path, to add images from your system
encoded_image = weaviate.util.image_encoder_b64(f"D:/W/weaviate-examples/vegetable-classification/train/{fol}/{img}")
data_properties = {
"labelName": fol,
"image": encoded_image
}
client.data_object.create(data_properties, "Vegetables", generate_uuid('Vegetables',fol+img))
cnt-=1
if cnt==0:break
print(f"100 Vegetable images from {fol} added.")
print("Images added")