You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

detect.py 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import cv2
  2. import numpy as np
  3. watch_cascade = cv2.CascadeClassifier('./model/cascade.xml')
  4. def computeSafeRegion(shape,bounding_rect):
  5. top = bounding_rect[1] # y
  6. bottom = bounding_rect[1] + bounding_rect[3] # y + h
  7. left = bounding_rect[0] # x
  8. right = bounding_rect[0] + bounding_rect[2] # x + w
  9. min_top = 0
  10. max_bottom = shape[0]
  11. min_left = 0
  12. max_right = shape[1]
  13. # print "computeSateRegion input shape",shape
  14. if top < min_top:
  15. top = min_top
  16. # print "tap top 0"
  17. if left < min_left:
  18. left = min_left
  19. # print "tap left 0"
  20. if bottom > max_bottom:
  21. bottom = max_bottom
  22. #print "tap max_bottom max"
  23. if right > max_right:
  24. right = max_right
  25. #print "tap max_right max"
  26. # print "corr",left,top,right,bottom
  27. return [left,top,right-left,bottom-top]
  28. def cropped_from_image(image,rect):
  29. x, y, w, h = computeSafeRegion(image.shape,rect)
  30. return image[y:y+h,x:x+w]
  31. def detectPlateRough(image_gray,resize_h = 720,en_scale =1.08 ,top_bottom_padding_rate = 0.05):
  32. print(image_gray.shape)
  33. if top_bottom_padding_rate>0.2:
  34. print("error:top_bottom_padding_rate > 0.2:",top_bottom_padding_rate)
  35. exit(1)
  36. height = image_gray.shape[0]
  37. padding = int(height*top_bottom_padding_rate)
  38. scale = image_gray.shape[1]/float(image_gray.shape[0])
  39. image = cv2.resize(image_gray, (int(scale*resize_h), resize_h))
  40. image_color_cropped = image[padding:resize_h-padding,0:image_gray.shape[1]]
  41. image_gray = cv2.cvtColor(image_color_cropped,cv2.COLOR_RGB2GRAY)
  42. watches = watch_cascade.detectMultiScale(image_gray, en_scale, 2, minSize=(36, 9),maxSize=(36*40, 9*40))
  43. cropped_images = []
  44. for (x, y, w, h) in watches:
  45. cropped_origin = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
  46. x -= w * 0.14
  47. w += w * 0.28
  48. y -= h * 0.6
  49. h += h * 1.1;
  50. cropped = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
  51. cropped_images.append([cropped,[x, y+padding, w, h],cropped_origin])
  52. return cropped_images