Advertisement
Guest User

Shitty python code that I can not understand

a guest
Feb 23rd, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. def contour_img(img_path,thresh=400,std_dev=4, hsv_lower=[22, 30, 30], hsv_upper=[45, 255, 255]):
  2. """Returns the name of the saved contour PNG image that will be sent for OCR thru API"""
  3. contoured_img = "contoured_"+os.path.basename(img_path).split(".")[0]+".png"
  4. image = cv2.imread(img_path)
  5.  
  6. # rgb to HSV color spave conversion
  7. hsv_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  8.  
  9. HSV_lower = np.array(hsv_lower, np.uint8) # Lower HSV value
  10. HSV_upper = np.array(hsv_upper, np.uint8) # Upper HSV value
  11.  
  12. frame_threshed = cv2.inRange(hsv_img, HSV_lower, HSV_upper)
  13. # find connected components
  14. _, contours, hierarchy, = cv2.findContours(frame_threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  15.  
  16. # Draw contours around filtered objects
  17. OutputImg = image.copy()
  18. cnt_lens = [cv2.contourArea(x) for x in contours]
  19.  
  20. klist = cnt_lens
  21. avglatlist = range(0,len(cnt_lens))
  22.  
  23. klist_np = np.array(klist).astype(np.float)
  24. avglatlist_np = np.array(avglatlist).astype(np.float)
  25.  
  26. # klist_filtered = klist_np[(abs(klist_np - np.mean(klist_np))) > (std_dev * np.std(klist_np))]
  27. avglatlist_filtered = avglatlist_np[(abs(klist_np - np.mean(klist_np))) > (std_dev * np.std(klist_np))]
  28. while len(avglatlist_filtered)==0:
  29. if std_dev<1.5:
  30. return False
  31. break
  32. std_dev=std_dev-0.5
  33. avglatlist_filtered = avglatlist_np[(abs(klist_np - np.mean(klist_np))) > (std_dev * np.std(klist_np))]
  34.  
  35. max_thresh = max(cnt_lens)
  36. mask = np.zeros_like(image) # Create mask where white is what we want, black otherwise
  37. out = np.zeros_like(image) # Extract out the object and place into output image
  38.  
  39. for c in avglatlist_filtered:
  40. # # remove noise objects having contour length threshold value
  41. cnt = contours[int(c)]
  42.  
  43. if len(cnt) > thresh:
  44. expand_cnt = expand_contour(cnt,expand_rate_x=1.02,expand_rate_y=1.04)
  45. # smooth_cnt = smoothen_contour(expand_cnt)
  46. epsilon = 0.001*cv2.arcLength(cnt,True)
  47. print "Epsilon", epsilon
  48. smooth_cnt = cv2.approxPolyDP(expand_cnt, epsilon, False)
  49. # cust_smooth_cnt = custom_smoothen_cnt(cnt)
  50. cv2.drawContours(OutputImg, [cnt], 0, (0, 0, 50), 1)
  51. cv2.drawContours(OutputImg, [expand_cnt], 0, (0, 0, 255), 2)
  52. cv2.drawContours(OutputImg, [smooth_cnt], 0, (255, 0, 0), 2)
  53. # cv2.drawContours(OutputImg, [smooth_cnt], 0, (0, 255,0), 1)
  54. cv2.drawContours(mask, [smooth_cnt], 0, (255,255,255), -1) # Draw filled contour in mask
  55. cv2.drawContours(mask, [expand_cnt], 0, (255, 255, 255), -1) # Draw filled contour in mask
  56.  
  57.  
  58. # hull = cv2.convexHull(cnt)
  59.  
  60. out[mask == 255] = image[mask == 255]
  61. out[mask == 0] = 255
  62. imgray = cv2.cvtColor(out, cv2.COLOR_BGR2GRAY)
  63.  
  64. #save contoured image to display
  65. j = Image.fromarray(imgray)
  66. j.save(contoured_img)
  67.  
  68. cv2.imwrite("col_"+contoured_img,OutputImg)
  69. return contoured_img
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement