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

66 lines
2.1 KiB

6 years ago
  1. import cv2
  2. import time
  3. import os
  4. import numpy as np
  5. vehicles = ["ambulance", "car1"]
  6. sides = [str(x) for x in range(0,361,30)]
  7. distances =["close_high","far_high","close_low","far_low"]
  8. cam_no = 0
  9. while 1:
  10. cam = cv2.VideoCapture(cam_no)
  11. if not (cam is None or not cam.isOpened()):
  12. break
  13. cam_no+=1
  14. def take_image():
  15. for vehicle in vehicles:
  16. if vehicle == "ambulance":
  17. continue
  18. if not os.path.exists("images\\"+vehicle):
  19. os.makedirs("images\\"+vehicle)
  20. for distance in distances:
  21. for side in sides:
  22. for i in range(1,3):
  23. ret,img= cam.read()
  24. cv2.imwrite("images\\{}\\{}-{}({}).jpg".format(vehicle,distance,side,i),img)
  25. cv2.imshow("current",img)
  26. ex_c = [27, ord("q"), ord("Q")]
  27. if cv2.waitKey(1) & 0xFF in ex_c:
  28. break
  29. print("Took side {}:distance:{}, waiting 7 seconds".format(side,distance))
  30. time.sleep(7)
  31. print("Finished distance:"+distance)
  32. while not cv2.waitKey(1) & 0xFF in ex_c:
  33. ret,img= cam.read()
  34. cv2.imshow("current",img)
  35. print("Finished vehicle:"+vehicle)
  36. while not cv2.waitKey(1) & 0xFF in ex_c:
  37. ret,img= cam.read()
  38. cv2.imshow("current",img)
  39. def cut_image():
  40. for vehicle in vehicles:
  41. images = []
  42. image_main = None
  43. if not os.path.exists("images\\"+vehicle):
  44. os.makedirs("images\\"+vehicle)
  45. for distance in distances:
  46. for side in sides:
  47. for i in range(1,3):
  48. img = cv2.imread("images\\{}\\{}-{}({}).jpg".format(vehicle,distance,side,i))
  49. images.append(img)
  50. image_main = np.zeros_like(images[0])
  51. sums = np.array(image_main, dtype='int64')
  52. for i in range(len(images)):
  53. sums += np.array(images[i],dtype='int64')
  54. image_main = np.array(sums/(len(images)+1)).astype(uint8)
  55. cv2.imshow("a",image_main)
  56. cv2.waitKey(0)
  57. cut_image()