-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
118 lines (102 loc) · 2.6 KB
/
functions.py
File metadata and controls
118 lines (102 loc) · 2.6 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
import requests
from PIL import Image
import json
import os
from database import Insert_Item, ImgBlob
def img_recon(img):
session = requests.Session()
URL = "http://image-dnn-sgh-jpbarraca.ws.atnog.av.it.pt/process"
with open(img, "rb") as f:
file = {'img': f.read()}
r = session.post(url=URL, files=file, data=dict(thr=0.5))
if r.status_code == 200:
return r.json()
list2 = []
list_rgb2 = []
list_conf2 = []
def img_crop(img):
im = Image.open(img)
sep = img.split(".")
sep2 = sep[0].split("/")
im2 = Image.new(im.mode, im.size)
list1, list_conf2 = img_coords(img)
j = 1
i = 0
list2 = []
list_rgb2 = []
while i <= len(list1)-1:
box = list1[i]
name = list1[i+1]
filer = "croppedImages/"+str(name)
if not os.path.exists(filer):
os.makedirs("croppedImages/"+str(name))
os.makedirs("Webpage/croppedImages/"+str(name))
#im2 = Image.new(im.crop(box).mode, im.crop(box).size)
im2 = im.crop(box)
im3 = im.crop(box)
nome = sep2[1]+"_"+str(j)+"."+sep[1]
a = "croppedImages/"+str(name)+"/"+nome
b = "Webpage/"+a
im2.save(a)
im3.save(b)
list_rgb2.append(img_frequent_color(a))
Insert_Item(ImgBlob(a), list_conf2[j-1], a, list_rgb2[j-1],str(img))
i += 2
j += 1
list2.append(a)
def img_coords(img):
a = img_recon(img)
list1 = []
list_conf = []
for obj in a:
b = (obj["box"]["x"], obj["box"]["y"], obj["box"]["x1"], obj["box"]["y1"])
list1.append(b)
list1.append(obj["class"])
list_conf.append(int(100*obj["confidence"]))
return list1, list_conf
def img_frequent_color(img):
s=""
im2 = Image.open(img)
width, height = im2.size
im = im2.convert('RGB')
m = im.mode
list_rgb = []
r_t = 0
g_t = 0
b_t = 0
b2_t = 0
w_t = 0
s=""
list_rgb = []
for x in range(width):
for y in range(height):
p = im.getpixel((x,y))
if(p[0] < 50 and p[1] < 50 and p[2] < 50):
b2_t += 1
elif((p[0] > 200 and p[2] > 200) or(p[0] > 200 and p[1] > 200 ) or(p[1] > 200 and p[2] > 200) ):
w_t += 1
elif(p[0] > p[1] and p[0]> p[2]):
r_t += 1
elif(p[1] > p[0] and p[1]>p[2]):
g_t += 1
elif(p[2] > p[0] and p[2]>p[1]):
b_t += 1
'''print(r_t)
print(g_t)
print(b_t)
print(b2_t)
print(w_t)
print("###########################")'''
if(r_t > g_t and r_t >b_t and r_t >b2_t and r_t >w_t):
s = "red"
elif(g_t > r_t and g_t >b_t and g_t >b2_t and g_t >w_t):
s = "green"
elif(b_t > g_t and b_t >r_t and b_t >b2_t and b_t >w_t):
s = "blue"
elif(b2_t > g_t and b2_t >r_t and b2_t >b_t and b2_t >w_t):
s = "black"
elif(w_t > g_t and w_t >r_t and w_t >b2_t and w_t >b_t):
s = "white"
else:
s="no dominant colour"
return s