Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. def add_data(file_name, to_show = False):
  2. img = cv2.imread(file_name)
  3. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  4. size = 700
  5. #im = image.load_img('10.jpg', target_size = (size, size))
  6. #img = 255 - img
  7. #show(img)
  8. #img[img<100]=0
  9.  
  10. img = cv2.resize(img, (size, size))
  11. im = np.array(img)
  12. #img = 1 - img
  13. gray = grayscale(img)
  14. blur = gaussian_blur(gray, 7)
  15. edge = canny(blur, 50, 300)
  16.  
  17. img = np.array(im)
  18. l = 0;
  19. r = 300
  20.  
  21. #print(lines is None)
  22. while l + 1 != r:
  23. m = (l + r)//2
  24. #print(m)
  25. lines = cv2.HoughLines(edge, 1, np.pi/180, m)
  26. size = 0;
  27. if(not(lines is None)): size = len(lines)
  28. if size > 10:
  29. l = m
  30. else:
  31. r = m
  32. if len(lines) < 2:
  33. return -1
  34. #print(l)
  35. #print(lines.shape)
  36. def gety(p):
  37. a = np.cos(p[1])
  38. b = np.sin(p[1])
  39. y0 = b*p[0]
  40. y1 = int(y0)
  41. return y1
  42.  
  43. def gety2(p):
  44. a = np.cos(p[1])
  45. b = np.sin(p[1])
  46. x0 = a*p[0]
  47. y0 = b*p[0]
  48. y2 = int(y0 - img.shape[0]*(a))
  49. return y2
  50.  
  51. def find_nearest(array, value):
  52. array = np.asarray(array)
  53. idx = (np.abs(array - value)).argmin()
  54. return array[idx]
  55.  
  56. mean_angle = lines[:,:,1].mean()
  57. eps = np.pi / 10;
  58. new_lines = []
  59. for i in lines:
  60. if abs(i[:,1] - mean_angle) < eps:
  61. new_lines.append(i)
  62. lines = np.array(new_lines)
  63. if len(lines) < 2:
  64. return -1
  65.  
  66. new_lines = []
  67. hight = []
  68. for i in lines:
  69. hight.append(gety(i[0]))
  70. dist = []
  71. hight.sort()
  72. for i in range(len(hight) - 1):
  73. dist.append(hight[i + 1] - hight[i])
  74. mean_hi = np.array(dist).mean() / 1.2
  75. #mean_hi = 50
  76. y_cord = []
  77. #print(mean_hi)
  78. for i in lines:
  79. y = gety(i[0])
  80. if len(y_cord) == 0 or abs(find_nearest(np.array(y_cord), y) - y) > mean_hi:
  81. new_lines.append(i)
  82. y_cord.append(y)
  83. lines = np.array(new_lines)
  84. if len(lines) < 2:
  85. return -1
  86. hight = []
  87. for i in lines:
  88. hight.append(gety(i[0]))
  89. dist = []
  90. hight.sort()
  91. for i in range(len(hight) - 1):
  92. dist.append(hight[i + 1] - hight[i])
  93. min_dist = np.array(dist).min()
  94. lines = np.array(sorted(lines, key=lambda x: gety(x[0])))
  95. if len(lines) < 2:
  96. return -1
  97.  
  98. add_lines = []
  99. for i in range(lines.shape[0] - 1):
  100. dist = gety(lines[i + 1][0]) - gety(lines[i][0])
  101. mean_angle = (lines[i + 1][0][1] + lines[i][0][1])/2;
  102.  
  103. num = int(round(dist / min_dist)) - 1;
  104. curr_d = dist / (num + 1)
  105. for j in range(num):
  106. line = np.array(lines[i])
  107. line[0][0] += (j + 1) * curr_d;
  108. line[0][1] = mean_angle;
  109. add_lines.append(line)
  110.  
  111. for i in lines:
  112. add_lines.append(i)
  113. lines = np.array(add_lines)
  114. #print(lines.shape)
  115. lines = np.array(sorted(lines, key=lambda x: gety(x[0])))
  116.  
  117. #!!!!!!!!!!!!!!!
  118. step = min_dist // 3
  119. color_vector = []
  120. product_data = []
  121. all_data = []
  122. amount_shelfs = 0;
  123. for i in range(lines.shape[0] - 1):
  124. #break
  125. y1 = gety(lines[i][0])
  126. y2 = gety2(lines[i][0])
  127. Y1 = gety(lines[i + 1][0])
  128. Y2 = gety2(lines[i + 1][0])
  129. #print('y1 = ', y1, " Y1 = ", Y1)
  130. center1 = (y1 + Y1) // 2;
  131. center2 = (y2 + Y2) // 2;
  132. #print('center1 = ', center1)
  133. ##!!!!!!!!!!!!!!
  134. d = int(((Y1 - y1 + Y2 - y2) / 4 / 1.3))
  135. #print('d = ', d)
  136. j = 0;
  137. shelf = []
  138. while j < img.shape[0] - step:
  139. x1 = j;
  140. x2 = j + step;
  141. y = j*(center2 - center1) // img.shape[0] + center1;
  142. y_1 = y - d;
  143. y_2 = y + d
  144. #numpy.mean(image_array, axis=0)`
  145. #print(x1, x2, y_1, y_2)
  146. curr = []
  147. vec = im[y_1:y_2, x1:x2]
  148. h = (y_2 - y_1)//3
  149. color = np.median(vec, axis=(0, 1))
  150. color2 = np.mean(vec, axis=(0, 1))
  151.  
  152. for k in range(3):
  153. curr.append(color[k])
  154. curr.append(color2[k])
  155. max1 = np.max(vec[:,:,k])
  156.  
  157. for u in range(3):
  158. vec1 = vec[u*h: (u + 1)*h, : ]
  159. color = np.median(vec1, axis=(0, 1))
  160. color2 = np.mean(vec1, axis=(0, 1))
  161.  
  162. for k in range(3):
  163. curr.append(color[k])
  164. curr.append(color2[k])
  165. max1 = np.max(vec1[:,:,k])
  166.  
  167.  
  168. #print(color.shape)
  169. #img[y_1:y_2, x1:x2] = color
  170. if len(curr) > 0:
  171. all_data.append(np.array(curr))
  172. shelf.append(np.array(curr))
  173. #cv2.rectangle(img,(x1,x2),(y_1, y_2),(0,255,0),1)
  174. #print(color)
  175. j += step
  176. cv2.rectangle(img,(x1,y_1),(x2,y_2),(0,255,0),1)
  177. #break;
  178. #print("")
  179. product_data.append(np.array(shelf))
  180. #break;
  181.  
  182.  
  183. product_data = np.array(product_data)
  184.  
  185. for j in lines:
  186. for rho,theta in j:
  187. #print(theta)
  188. a = np.cos(theta)
  189. b = np.sin(theta)
  190. x0 = a*rho
  191. y0 = b*rho
  192. x1 = int(x0 + 1000*(-b))
  193. y1 = int(y0 + 1000*(a))
  194. x2 = int(x0 - 1000*(-b))
  195. y2 = int(y0 - 1000*(a))
  196. #print(y1)
  197. cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
  198.  
  199. #cv2.imwrite('houghlines3.jpg',img)
  200. if(to_show): show(img)
  201. return all_data
  202. #color_vector = color_vector.astype(int)
  203. #print(product_data.shape)
  204. #color_vector[0][0] = 255
  205. #color_vector[-1,:, 0] = 255
  206. #print(color_vector[:5][:5])
  207. #show(color_vector)
  208. #print(color_vector.shape)
  209. #print(img.shape)
  210. def get_arrage_status(name):
  211. model1 = CatBoostClassifier()
  212. model1.load_model("catboost_detect")
  213. a = add_data(name)
  214. if a == -1:
  215. return 0
  216. if len(a) <2:
  217. return 0
  218. img_data = np.array(a)
  219. img_data /= 255
  220. out_pred = model.predict_proba(img_data)
  221. s = ""
  222. uv = []
  223. out = ''
  224. for i in out_pred:
  225. #print(i)
  226. s += str(i.argmax())
  227. uv.append(i.max())
  228. k = len(s) // num
  229. #print(len(s))
  230. l = 0
  231. s += '9'
  232. r = 0
  233. c = '9'
  234. arr = []
  235. for i in s:
  236. if(i == '0'):
  237. c = '9'
  238. if i!=c:
  239. arr.append(l)
  240. c = i;
  241. l = 1
  242. else:
  243. l += 1
  244. arr.sort()
  245. arr.reverse()
  246. metric = np.array(arr[:5]).sum()
  247. return metric
  248. #print(metric)
  249. # for i in range(num):
  250. # for j in range(k):
  251. # out += s[r]
  252. # r+=1
  253. # out +='\n'
  254.  
  255. # print(out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement