-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (118 loc) · 5.69 KB
/
main.py
File metadata and controls
151 lines (118 loc) · 5.69 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import json
import cv2
import csv
# change apikey with yours
url = 'https://api.chooch.ai/predict/image?apikey=8b534527-eb6b-4293-b653-de6eee231882'
input_image = "input3.jpeg"
input_image_directory = "./input/" + input_image
files = {'image': open(input_image_directory, 'rb')}
response = requests.post(url, files=files)
#print(response.content)
json_data = json.loads(response.text)
# initialize csv files
with open('object_detection.csv', 'w') as f:
writer = csv.writer(f)
csv_line = "object, x1, x2, y1, y2"
writer.writerows([csv_line.split(',')])
with open('text_detection.csv', 'w') as f:
writer = csv.writer(f)
csv_line = "text, x1, x2, y1, y2"
writer.writerows([csv_line.split(',')])
# write data to csv function
def write_csv (data1, data2, data3, data4, data5, detection_category):
if (detection_category == "object_detection"):
with open('object_detection.csv', 'a') as f:
writer = csv.writer(f)
csv_line = data1 + "," + data2 + "," + data3 + "," + data4 + "," + data5
writer.writerows([csv_line.split(',')])
if (detection_category == "text_detection"):
with open('text_detection.csv', 'a') as f:
writer = csv.writer(f)
csv_line = data1 + "," + data2 + "," + data3 + "," + data4 + "," + data5
writer.writerows([csv_line.split(',')])
# crop the detected objects and texts
def crop_custom(img, x1, y1, x2, y2): # crop custom
return img[y1:y2, x1:x2]
# font to draw text on output image
font = cv2.FONT_HERSHEY_SIMPLEX
src_img = cv2.imread(input_image_directory)
img = cv2.imread(input_image_directory)
detection_data = {}
counter = 0
# parse the detected object names and pixel locations
for i in json_data["objects"]["predictions"]:
if i["object_title"] in detection_data:
detection_data[i["object_title"] +"_" + str(counter)] = i["coordinates"]
x1 = str(i["coordinates"]).split(",")[0]
x2 = str(i["coordinates"]).split(",")[1]
y1 = str(i["coordinates"]).split(",")[2]
y2 = str(i["coordinates"]).split(",")[3]
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,255,0), 2)
cv2.putText(img, i["object_title"], (int(x1),int(y1)-10), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
counter = counter + 1
write_csv (str(i["object_title"]), x1, x2, y1, y2, "object_detection")
cropped_image = crop_custom(src_img, int(x1), int(y1), int(x2), int(y2))
cv2.imwrite("./detected_objects/" + input_image.split('.')[0] + "_" + str(i["object_title"]) + ".png", cropped_image)
else:
detection_data[i["object_title"]] = i["coordinates"]
x1 = str(i["coordinates"]).split(",")[0]
x2 = str(i["coordinates"]).split(",")[1]
y1 = str(i["coordinates"]).split(",")[2]
y2 = str(i["coordinates"]).split(",")[3]
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,255,0), 2)
cv2.putText(img, i["object_title"], (int(x1),int(y1)-10), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
write_csv (str(i["object_title"]), x1, x2, y1, y2, "object_detection")
cropped_image = crop_custom(src_img, int(x1), int(y1), int(x2), int(y2))
cv2.imwrite("./detected_objects/" + input_image.split('.')[0] + "_" + str(i["object_title"]) + ".png", cropped_image)
# reset the dictionary and the counter
detection_data = {}
counter = 0
# parse the detected texts and pixel locations
for i in json_data["texts"]["predictions"]:
try:
if i["text_value"] in detection_data and i["coordinates"] != None:
detection_data[i["text_value"] +"_" + str(counter)] = i["coordinates"]
x1 = str(i["coordinates"]).split(",")[0]
x2 = str(i["coordinates"]).split(",")[1]
y1 = str(i["coordinates"]).split(",")[2]
y2 = str(i["coordinates"]).split(",")[3]
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2)
cv2.putText(img, i["text_value"], (int(x1),int(y1)-10), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
counter = counter + 1
write_csv (str(i["text_value"]), x1, x2, y1, y2, "text_detection")
cropped_image = crop_custom(src_img, int(x1), int(y1), int(x2), int(y2))
cv2.imwrite("./detected_texts/" + input_image.split('.')[0] + "_" + str(i["text_value"]) + ".png", cropped_image)
else:
if i["coordinates"] != None:
detection_data[i["text_value"]] = i["coordinates"]
x1 = str(i["coordinates"]).split(",")[0]
x2 = str(i["coordinates"]).split(",")[1]
y1 = str(i["coordinates"]).split(",")[2]
y2 = str(i["coordinates"]).split(",")[3]
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2)
cv2.putText(img, i["text_value"], (int(x1),int(y1)-10), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
write_csv (str(i["text_value"]), x1, x2, y1, y2, "text_detection")
cropped_image = crop_custom(src_img, int(x1), int(y1), int(x2), int(y2))
cv2.imwrite("./detected_texts/" + input_image.split('.')[0] + "_" + str(i["text_value"]) + ".png", cropped_image)
except:
print("ok")
# reset the dictionary and the counter
detection_data = {}
counter = 0
# parse the detected objects and total counts of them
for i in json_data["objects"]["summary"]:
try:
if i["object_title"] in detection_data and i["count"] != None:
detection_data[i["object_title"] +"_" + str(counter)] = i["count"]
counter = counter + 1
else:
if i["count"] != None:
detection_data[i["object_title"]] = i["count"]
except:
print("ok")
print("-> Detected objects with counts: " + str(detection_data))
# draw texts on output image and save it as a new image
cv2.putText(img, str(detection_data), (20,35), font, 1, (255, 0, 0), 2, cv2.LINE_AA)
cv2.imwrite("./output/" + input_image.split('.')[0]+"_output.png",img)
print("-> All the processes are done. The output image was saved in here: " + "./output/" + input_image.split('.')[0]+"_output.png")