Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. class Processor(object):
  2. #Constructor
  3. def __init__(self,exec_net,input_blob,out_blob,n,c,h,w):
  4. self.exec_net=exec_net
  5. self.input_blob=input_blob
  6. self.out_blob=out_blob
  7. self.n=n
  8. self.c=c
  9. self.h=h
  10. self.w=w
  11.  
  12. def process_frame(self,cur_request_id,next_request_id,frame,frame_height,frame_width,is_async_mode):
  13.  
  14. in_frame = cv2.resize(frame, (self.w, self.h))
  15. in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
  16. in_frame = in_frame.reshape((self.n, self.c, self.h, self.w))
  17.  
  18. if is_async_mode:
  19. self.exec_net.start_async(request_id=next_request_id, inputs={self.input_blob: in_frame})
  20. else:
  21. self.exec_net.start_async(request_id=cur_request_id, inputs={self.input_blob: in_frame})
  22.  
  23. if self.exec_net.requests[cur_request_id].wait(-1) == 0:
  24. #Parse detection results of the current request
  25. res = self.exec_net.requests[cur_request_id].outputs[self.out_blob]
  26. return res
  27.  
  28.  
  29. def placeBoxes(self,res, labels_map, prob_threshold, frame, initial_w, initial_h, is_async_mode, cur_request_id):
  30. count=0
  31. for obj in res[0][0]:
  32. # Draw only objects when probability more than specified threshold
  33. if obj[2] > prob_threshold:
  34. xmin = int(obj[3] * initial_w)
  35. ymin = int(obj[4] * initial_h)
  36. xmax = int(obj[5] * initial_w)
  37. ymax = int(obj[6] * initial_h)
  38. class_id = int(obj[1])
  39. if class_id ==12 or class_id == 13 or class_id == 14 or class_id == 9:
  40. count=count+1
  41. # Draw box and label\class_id
  42. '''
  43. inf_time_message = "Inference time: N\A for async mode" if is_async_mode else \
  44. "Inference time: {:.3f} ms".format(det_time * 1000)
  45. async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) if is_async_mode else \
  46. "Async mode is off. Processing request {}".format(cur_request_id)
  47. '''
  48. color = (min(class_id * 12.5, 255), min(class_id * 7, 255), min(class_id * 5, 255))
  49. cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
  50. det_label = labels_map[class_id] if labels_map else str(class_id)
  51. cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0,0,255), 1)
  52. #cv2.putText(frame, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
  53. #cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5,(10, 10, 200), 1)
  54.  
  55. return frame,count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement