Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import json
  4. import os
  5.  
  6.  
  7. def set_green(path):
  8. cap = cv2.VideoCapture(path)
  9. ret, frame1 = cap.read()
  10. cv2.circle(frame1, (40, 500), 20, (0, 255, 0), -1)
  11. cv2.imwrite('frame_change.png', frame1)
  12. return frame1
  13.  
  14.  
  15. def draw_circle(x, y, flow, img):
  16. height, width, _ = img.shape
  17. x = x + flow[int(y)][int(x)][0]
  18. y = y + flow[int(y)][int(x)][1]
  19. cv2.circle(img, (int(x), int(y)), 20, (0, 255, 0), -1)
  20. cv2.imshow('green', img)
  21. return x, y
  22.  
  23.  
  24. def print_track(path):
  25. cap = cv2.VideoCapture(path)
  26. ret, frame1 = cap.read()
  27. prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
  28. hsv = np.zeros_like(frame1)
  29. hsv[..., 1] = 255
  30. while (1):
  31. ret, frame2 = cap.read()
  32. cv2.imwrite("framefirst.png", frame1)
  33. cv2.imshow('frame_smth', frame2)
  34. next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
  35. flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
  36. mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
  37. hsv[..., 0] = ang * 180 / np.pi / 2
  38. hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
  39. bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
  40. cv2.imshow('frame2', bgr)
  41. k = cv2.waitKey(30) & 0xff
  42. if k == 27:
  43. break
  44. elif k == ord('s'):
  45. cv2.imwrite('opticalfb.png', frame2)
  46. cv2.imwrite('opticalhsv.png', bgr)
  47. prvs = next
  48. cap.release()
  49. cv2.destroyAllWindows()
  50.  
  51.  
  52. def track_green_circle(path): # track specified region in the image
  53. cap = cv2.VideoCapture(path)
  54. frame1 = set_green(path)
  55. prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
  56. hsv = np.zeros_like(frame1)
  57. hsv[..., 1] = 255
  58. x, y = 40, 500
  59. while (1):
  60. ret, frame2 = cap.read()
  61. cv2.imwrite("framefirst.png", frame1)
  62. cv2.imshow('frame_smth', frame2)
  63. next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
  64. flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
  65. print(flow.shape)
  66. x, y = draw_circle(x, y, flow, frame2)
  67. mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
  68. hsv[..., 0] = ang * 180 / np.pi / 2
  69. hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
  70. bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
  71. cv2.imshow('frame2', bgr)
  72. k = cv2.waitKey(30) & 0xff
  73. if k == 27:
  74. break
  75. elif k == ord('s'):
  76. cv2.imwrite('opticalfb.png', frame2)
  77. cv2.imwrite('opticalhsv.png', bgr)
  78. prvs = next
  79. cap.release()
  80. cv2.destroyAllWindows()
  81.  
  82.  
  83. def inside(point, coord, img):
  84. x, y = img.shape
  85. if (not coord):
  86. return point >= 0 and point <= x
  87. return point >= 0 and point <= y
  88. pass
  89.  
  90.  
  91. def create_test_image(path,
  92. change): # we give out image and draw circles with some certain optical flow in vector change(x, y) when we reach the end *= -1 return correct_optical_flow
  93. cap = cv2.VideoCapture(path)
  94. ret, frame = cap.read()
  95. float = []
  96. x, y, r = 40, 500, 20
  97. fourcc = cv2.VideoWriter_fourcc(*'mp4')
  98. fps = cap.get(cv2.CV_CAP_PROP_FPS)
  99. out = cv2.VideoWriter('green_circles_video.mp4', fourcc, fps, (frame.shape))
  100. while (not frame.empty()):
  101. cv2.circle(frame, (x, y), r, (0, 255, 0), -1)
  102. if (not inside((x + change[0]), 0, frame)):
  103. change[0] *= -1
  104. if (not inside(y + change[1], 1, frame)):
  105. change[1] *= -1
  106. float.append(change)
  107. x += change[0]
  108. y += change[1]
  109. pass
  110.  
  111.  
  112. def drawOptFlowMap(flow, prvs, step, _, color):
  113. cflowmap = cv2.cvtColor(prvs, cv2.COLOR_GRAY2BGR)
  114. for y in range(0, cflowmap.shape[0], step):
  115. for x in range(0, cflowmap.shape[1], step):
  116. fxy = flow[y][x]
  117. cv2.line(cflowmap, (x, y), (int(x + fxy[0]), int(y + fxy[1])), color)
  118. cv2.circle(cflowmap, (x, y), 2, color, -1)
  119. cv2.imshow("flow", cflowmap)
  120.  
  121.  
  122. def draw_hsv(flow, frame1):
  123. hsv = np.zeros_like(frame1)
  124. hsv[..., 1] = 255
  125. mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
  126. hsv[..., 0] = ang * 180 / np.pi / 2
  127. hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
  128. bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
  129. cv2.imshow('frame2', bgr)
  130. pass
  131.  
  132.  
  133. def track_map(path): # with drawOptFlowMap
  134. cap = cv2.VideoCapture(path)
  135. ret, frame1 = cap.read()
  136. prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
  137. while (1):
  138. ret, frame2 = cap.read()
  139. next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
  140. flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
  141. drawOptFlowMap(flow, prvs, 16, 1.5, (0, 255, 0))
  142. draw_hsv(flow, frame1)
  143. k = cv2.waitKey(30) & 0xff
  144. if k == 27:
  145. break
  146. elif k == ord('s'):
  147. cv2.imwrite('opticalfb.png', frame2)
  148. #cv2.imwrite('opticalhsv.png', bgr)
  149. prvs = next
  150. cap.release()
  151. cv2.destroyAllWindows()
  152. #####################################################################
  153.  
  154.  
  155. def find(path, number):
  156. return os.path.isfile(make_name(path, number))
  157.  
  158.  
  159. def make_name(path, number):
  160. s = str(number)
  161. k = len(s)
  162. zer = ''
  163. for i in range(0, 6 - k):
  164. zer += '0'
  165. end = 'rc.jpg.json'
  166. res = path + zer + s + end
  167. return res
  168.  
  169.  
  170. def find_head(path, number):
  171. with open(make_name(path, number)) as json_data:
  172. d = json.load(json_data)
  173. d1 = d['objects']
  174. h = []
  175. for i in d1:
  176. for j in i['tags']:
  177. if j == 'h':
  178. h.append(i['data'])
  179. return h
  180.  
  181.  
  182. def compare(roi, ans):
  183. size = ans.shape.size
  184. res = []
  185. if size == 1:
  186. return compare_head(roi, ans)
  187. for i in range(size):
  188. res.append(compare_head(roi[i], ans[i]))
  189. return res
  190.  
  191.  
  192. def get_res(res):
  193. return res
  194.  
  195.  
  196. def compare_head(track, ans):
  197. res = 0
  198. s_track = set()
  199. s_ans = set()
  200. for i in track:
  201. s_track.add(i)
  202. for i in ans:
  203. s_ans.add(i)
  204. for i in s_ans:
  205. if s_track.find(i):
  206. res += 1
  207. return get_res(res)
  208.  
  209.  
  210. def upd(roi, flow):
  211. for i in range(roi.shape[0]):
  212. for j in range(roi.shape[1]):
  213. for u in range(roi.shape[2]):
  214. roi[i][j][u] = upd_cell(roi[i][j][u], flow)
  215. return roi
  216.  
  217.  
  218. def upd_cell(cell, flow): #we think that in json y, x not x, y
  219. cell[0] += flow[cell][1]
  220. cell[1] += flow[cell][0]
  221.  
  222. def fulfil(roi_border): #we think that in json y, x not x, y
  223. ans = []
  224. for i in roi_border:
  225. y1, x1, y2, x2 = i
  226. res = []
  227. for j in range(x1, x2 + 1):
  228. row = []
  229. for u in range(y1, y2 + 1):
  230. row.append([u, j])
  231. res.append(row)
  232. ans.append(res)
  233. return ans
  234.  
  235. def rewrittten_fulfil(roi_border): #we think that in json y, x not x, y
  236. y1, x1, y2, x2 = roi_border
  237. res = []
  238. for i in range(x1, x2 + 1):
  239. row = []
  240. for j in range(y1, y2 + 1):
  241. row.append([j, i])
  242. res.append(row)
  243. return res
  244.  
  245. def count_head(path):
  246. ans = 0
  247. with open(path) as json_data:
  248. d = json.load(json_data)
  249. for i in d['objects']:
  250. for j in i['tags']:
  251. if (j == 'h'):
  252. ans += 1
  253. return ans
  254.  
  255.  
  256. def add_head(roi, path):
  257. ans = 0
  258. with open(path) as json_data:
  259. d = json.load(json_data)
  260. for i in d['objects']:
  261. for j in i['tags']:
  262. if (j == 'h'):
  263. ans += 1
  264. if (ans > roi.shape[0]):
  265. roi.append(fulfil(i['data'])) # rewrite fulfil for one dimesion
  266. return roi
  267.  
  268.  
  269. def check_new_head(roi, path_js, ind):
  270. path = make_name(path_js, ind)
  271. if count_head(path) == roi.shape[0]:
  272. return roi
  273. roi = add_head(roi, path)
  274. return roi
  275.  
  276.  
  277. def track_head(path_vid, path_js):
  278. cap = cv2.VideoCapture(path_vid)
  279. ret, frame1 = cap.read()
  280. roi_border = find_head(path_js, 0)
  281. roi = fulfil(roi_border)
  282. prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
  283. ind = 1
  284. test = []
  285. while (1):
  286. ret, frame2 = cap.read()
  287. if frame2.empty:
  288. break
  289. next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
  290. flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)
  291. print(np.shape(flow))
  292. roi = upd(roi, flow)
  293. if find(path_js, ind):
  294. roi = check_new_head(roi, path_js, ind)
  295. ans = find_head(path_js, ind)
  296. test.append(compare(roi, ans))
  297. print(test)
  298.  
  299.  
  300.  
  301.  
  302. path = "/Users/denissurin/Downloads/Telegram Desktop/video_2018-07-13_15-56-11.mp4"
  303. path1 = "/Users/denissurin/Downloads/OneDrive-2018-07-18/real2.avi"
  304. path2 = "/Users/denissurin/Downloads/OneDrive-2018-07-18/record-0000-004C-20180505193003-20180505193014.avi"
  305. path3 = "/Users/denissurin/Downloads/OneDrive-2018-07-18/VNG_180621_3730.avi"
  306. path_to_json = "/Users/denissurin/Downloads/GPN_180319_6103/"
  307. path_to_video = "/Users/denissurin/Downloads/GPN_180319_6103.mp4"
  308. track_green_circle(path)
  309. # track_map(path3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement