def visualize(self): frame = self.frame font = cv2.FONT_HERSHEY_DUPLEX line_type = cv2.LINE_AA # draw and label blob bounding boxes for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) vehicle_label = 'I: ' + _id[:8] \ if blob.type is None \ else 'I: {0}, T: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) cv2.putText(frame, vehicle_label, (x, y - 5), font, 1, (255, 0, 0), 2, line_type) # draw counting lines for counting_line in self.counting_lines: cv2.line(frame, counting_line['line'][0], counting_line['line'][1], (255, 0, 0), 3) cl_label_origin = (counting_line['line'][0][0], counting_line['line'][0][1] + 35) cv2.putText(frame, counting_line['label'], cl_label_origin, font, 1, (255, 0, 0), 2, line_type) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) return frame
def visualize(self): frame = self.frame # draw and label blob bounding boxes for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) vehicle_label = 'ID: ' + _id[:8] \ if blob.type == None \ else 'ID: {0}, Type: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) cv2.putText(frame, vehicle_label, (x, y - 5), cv2.FONT_HERSHEY_DUPLEX, 1.2, (255, 0, 0), 2, cv2.LINE_AA) # draw counting line if self.counting_line != None: cv2.line(frame, self.counting_line[0], self.counting_line[1], (255, 0, 0), 3) # display vehicle count types_counts_str = ', '.join( [': '.join(map(str, i)) for i in self.types_counts.items()]) types_counts_str = ' (' + types_counts_str + ')' if types_counts_str != '' else types_counts_str cv2.putText(frame, 'Count: ' + str(self.vehicle_count) + types_counts_str, (20, 60), cv2.FONT_HERSHEY_DUPLEX, 1.5, (255, 0, 0), 2, cv2.LINE_AA) cv2.putText( frame, 'Processing speed: ' + str(self.processing_frame_rate) + ' FPS', (20, 120), cv2.FONT_HERSHEY_DUPLEX, 1.5, (255, 0, 0), 2, cv2.LINE_AA) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) return frame
def visualize(self): frame = self.frame # draw and label blob bounding boxes for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) a = (2 * x + w) / 2 # x,y更新了? b = (2 * y + h) / 2 #blob.center = ([a, b]) vehicle_label = 'ID: ' + _id[:8] \ if blob.type == None else 'Type: {0} ({1})'.format( blob.type, str(blob.type_confidence)[:4]) #else 'ID: {0}, Type: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) # change #file = open(_id+r".txt", "a") #point2 = (a+300, b+300) #points=np.loadtxt(_id + r".txt") #新添加的 path=r"D:\ZHOUSHENGHUA\Vehicle-Counting-master\Location"+'\\'+_id+r".txt" if os.path.exists(path): trace=np.append(np.loadtxt(path, delimiter=','),np.array([[a,b]]),axis=0,) recent_trace=trace[np.size(trace,0)-5:np.size(trace,0),:] print (recent_trace) np.savetxt(path, trace,delimiter=',') #print (trace) cv2.polylines(frame, np.int32([trace]), 0, (0, 0, 255),2) else: data=np.array([[a,b],[a,b]]) np.savetxt(path, data, delimiter=',') # 新添加的 # points = np.array(blob.center) #points=np.append([blob.center],[point2],axis=0) #points.append(blob.center,) #points.append([100,100]) # points.dtype => 'int64' #np.int32([points]) cv2.putText(frame, vehicle_label, (x, y - 5), cv2.FONT_HERSHEY_DUPLEX, 0.3, (255, 0, 0), 1, cv2.LINE_AA) #0.3字体大小,0.5字体粗细 #cv2.polylines(frame,[(300,300), (100,100),(200,500)], 0, (0, 0, 255)) # draw counting line if self.counting_line != None: cv2.line(frame, self.counting_line[0], self.counting_line[1], (255, 0, 0), 3) # display vehicle count types_counts_str = ', '.join([': '.join(map(str, i)) for i in self.types_counts.items()]) types_counts_str = ' (' + types_counts_str + ')' if types_counts_str != '' else types_counts_str cv2.putText(frame, 'Count: ' + str(self.vehicle_count) + types_counts_str, (20, 60), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 0, 0), 1, cv2.LINE_AA) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) return frame
def visualize(self): frame = self.frame font = cv2.FONT_HERSHEY_DUPLEX line_type = cv2.LINE_AA # draw and label blob bounding boxes for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), self.hud_color, 2) object_label = 'I: ' + _id[:8] \ if blob.type is None \ else 'I: {0}, T: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) cv2.putText(frame, object_label, (x, y - 5), font, 1, self.hud_color, 2, line_type) # draw counting lines for counting_line in self.counting_lines: p0 = counting_line['line'][0] p1 = counting_line['line'][1] cv2.line(frame, p0, p1, self.hud_color, 3) direction = counting_line.get('direction', None) if direction == 'left' or direction == 'right': p0_ = np.array(p0) p1_ = np.array(p1) if direction == 'left': pd_ = (p0_ + p1_) / 2 + ccw_rot.dot((p1_ - p0_) / 10) else: pd_ = (p0_ + p1_) / 2 - ccw_rot.dot((p1_ - p0_) / 10) pd = (int(pd_[0]), int(pd_[1])) cv2.line(frame, p0, pd, self.hud_color, 1) cv2.line(frame, p1, pd, self.hud_color, 1) cl_label_origin = (counting_line['line'][0][0], counting_line['line'][0][1] + 35) cv2.putText(frame, counting_line['label'], cl_label_origin, font, 1, self.hud_color, 2, line_type) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) # show counts if self.show_counts: offset = 1 for line, objects in self.counts.items(): cv2.putText(frame, line, (10, 40 * offset), font, 1, self.hud_color, 2, line_type) for label, count in objects.items(): offset += 1 cv2.putText(frame, "{}: {}".format(label, count), (10, 40 * offset), font, 1, self.hud_color, 2, line_type) offset += 2 return frame
def visualize(self): frame = self.frame font = cv2.FONT_HERSHEY_DUPLEX line_type = cv2.LINE_AA colors = [(255, 255, 255), (255, 255, 0), (255, 0, 0), (255, 0, 255), (0, 255, 255), (0, 255, 0), (0, 255, 255)] color_i = 0 # draw and label blob bounding boxes for _id, blob in self.blobs.items(): this_color = colors[color_i % len(colors)] color_i += 1 (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), this_color, 2) vehicle_label = 'I: ' + _id[:8] \ if blob.type is None \ else 'I: {0}, T: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) cv2.putText(frame, vehicle_label, (x, y - 5), font, 0.5, this_color, 1, line_type) # draw counting lines for counting_line in self.counting_lines: cv2.line(frame, counting_line['line'][0], counting_line['line'][1], (255, 0, 0), 3) cl_label_origin = (counting_line['line'][0][0], counting_line['line'][0][1] + 35) cv2.putText(frame, counting_line['label'], cl_label_origin, font, 1, (255, 0, 0), 2, line_type) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) # For each line, write the cumulative count on the video (note if you're detecting lots # of different classes, this may overflow off the bottom of the video). if self.draw_counts: offset = 1 for line, objects in self.counts.items(): cv2.putText(frame, line, (10, 40 * offset), font, 1, (255, 0, 0), 2, line_type) offset += 1 for label, count in objects.items(): cv2.putText(frame, "{}: {}".format(label, count), (10, 40 * offset), font, 1, (255, 0, 0), 2, line_type) offset += 1 return frame
def visualize(self): frame = self.frame font = cv2.FONT_HERSHEY_DUPLEX line_type = cv2.LINE_AA # draw and label blob bounding boxes for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] cv2.rectangle(frame, (x, y), (x + w, y + h), self.hud_color, 2) object_label = 'I: ' + _id[:8] \ if blob.type is None \ else 'I: {0}, T: {1} ({2})'.format(_id[:8], blob.type, str(blob.type_confidence)[:4]) cv2.putText(frame, object_label, (x, y - 5), font, 1, self.hud_color, 2, line_type) # draw counting lines for counting_line in self.counting_lines: cv2.line(frame, counting_line['line'][0], counting_line['line'][1], self.hud_color, 3) cl_label_origin = (counting_line['line'][0][0], counting_line['line'][0][1] + 35) cv2.putText(frame, counting_line['label'], cl_label_origin, font, 1, self.hud_color, 2, line_type) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi) # show counts if self.show_counts: offset = 1 for line, objects in self.counts.items(): cv2.putText(frame, line, (10, 40 * offset), font, 1, self.hud_color, 2, line_type) for label, count in objects.items(): offset += 1 cv2.putText(frame, "{}: {}".format(label, count), (10, 40 * offset), font, 1, self.hud_color, 2, line_type) offset += 2 return frame
def visualize(self, roi_setting_mode, is_paused, showUI, veh_light,ped_light,light_time, roi_setting_mode_P, line_setting_mode,line_setting_mode_P,vpoint_setting_mode,help_img_flag): frame = self.frame font = cv2. FONT_HERSHEY_COMPLEX line_type = cv2.LINE_AA occupys = [] offset = 0 lineDistance = distance_cal(self.counting_lines[0]['line'][0][1],self.camera_height,self.focal_length,self.pixel_length,self.resolution,self.vanishing_point) text1 = 'fps: ' +str(round(self.processing_frame_rate,2)) text2 = 'Now have: ' +str(len(self.blobs)) cv2.putText(frame, str(time.asctime( time.localtime(time.time()))), (0 , self.f_height-10), font, 1, (255, 255, 0), 1, line_type) get_text_box_with_background_color(frame,text1,(self.f_width - 300 , 50),font,1,(255, 255, 255),1,line_type,(0,0,0)) get_text_box_with_background_color(frame,text2,(self.f_width - 300 , 100),font,1,(255, 255, 255),1,line_type,(0,0,0)) # cv2.putText(frame, 'fps: ' +str(self.processing_frame_rate), (self.f_width - 150 , 50), font, 1, (25, 25, 25), 1, line_type) # cv2.putText(frame, 'Now have: ' +str(len(self.blobs))+' objects', (self.f_width - 350 , 100), font, 1, (25, 25, 25), 1, line_type) # draw and label blob bounding boxes self.veh_light = veh_light self.ped_light = ped_light frame = get_traffic_light_image(frame, self.detector, veh_light, ped_light) get_text_box_with_background_color(frame,str(light_time),(10 , 400),font,1,(255, 255, 255),1,line_type,(0,0,0)) for _id, blob in self.blobs.items(): (x, y, w, h) = [int(v) for v in blob.bounding_box] if blob.isJaywalker == True: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3) else: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3) for occupy in occupys: if y >= occupy[0] and y <= occupy[1]: offset+=1 if y-31 > occupy[0] and y-31 < occupy [1]: offset+=1 # cv2.putText(frame, 'ID:' + str(_id), (x , y - 25), font, 0.7, (0, 0, 0), 2, line_type) if self.detector == 'yolo': distanceToLine = blob.distance - lineDistance blob.distanceInLast3Frame.append(distanceToLine) if len(blob.distanceInLast3Frame) > 3: blob.distanceInLast3Frame.pop(0) if len(blob.distanceInLast3Frame) == 3: blob.mean_distanceInLast3Frame = list_equalization(blob.distanceBetween3Frames,abs(blob.distanceInLast3Frame[0] - blob.distanceInLast3Frame[2]),15) blob.remainingTime = cal_remaining_time(round(distanceToLine-blob.mean_distanceInLast3Frame,1),2,self.processing_frame_rate) if round(blob.remainingTime,1) > 0: blob.speed = round(distanceToLine/blob.remainingTime,1) get_text_box_with_background_color(frame,str(round(distanceToLine,1)) +'m '+ str(round(blob.remainingTime,1))+'s',(x , y - 5-(offset*35)),font,0.7,(0, 0, 0),2,line_type,(223,223,223)) if not self.veh_light == 0: if blob.speed >= 1.5 and distanceToLine <= 40: self.not_slowing_down_flag = True else: self.not_slowing_down_flag = False else: self.not_slowing_down_flag = False # +str(round(distanceToLine/blob.remainingTime*3.6,1))+' km/h' # cv2.putText(frame, str(round(distanceToLine,1)) +'m'+ str(round(blob.remainingTime,1))+'s', (x , y - 5), font, 0.7, (0, 0, 0), 2, line_type) else: if round(distanceToLine,1) > 0: get_text_box_with_background_color(frame,str(round(distanceToLine,1)) +'m ',(x , y - 5-(offset*35)),font,0.7,(0, 0, 0),2,line_type,(223,223,223)) # cv2.putText(frame, str(round(distanceToLine,1)) +'m' , (x , y - 5), font, 0.7, (0, 0, 0), 2, line_type) get_text_box_with_background_color(frame,'ID:' + str(_id),(x , y - 31 - (offset*35)),font,0.7,(0, 0, 0),2,line_type,(223,223,223)) cv2.line(frame, (x , y - 5-(offset*35)), (x,y), (255, 0, 0), 3) occupys.append((y - 31 - (offset*35), y - (offset*35))) else: get_text_box_with_background_color(frame,'ID:' + str(_id),(x , y - 5),font,0.7,(0, 0, 0),2,line_type,(223,223,223)) if blob.isJaywalker: get_text_box_with_background_color(frame,"Jaywalker detected.",(x , y - 30),font,0.7,(0, 0, 0),2,line_type,(223,223,223)) # draw counting lines for counting_line in self.counting_lines: cv2.line(frame, counting_line['line'][0], counting_line['line'][1], (255, 0, 0), 3) # cl_label_origin = (counting_line['line'][0][0], counting_line['line'][0][1] + 35) # cv2.putText(frame, str(round(self.blobDistance,1))+'m (' , cl_label_origin, font, 1, (255, 0, 0), 2, line_type) # ============================================================================= # for road in self.roads: # cv2.line(frame, road['line'][0], road['line'][1], (255, 0, 0), 3) # r_label_origin = (road['line'][1][0] - 15 , road['line'][1][1] - 35) # cv2.putText(frame, road['label'] , r_label_origin, font, 1, (255, 0, 0), 2, line_type) # ============================================================================= # ============================================================================= # for speed_estimation_line in self.speed_estimation_lines: # cv2.line(frame, speed_estimation_line['line'][0], speed_estimation_line['line'][1], (255, 0, 0), 3) # sel_label_origin = (speed_estimation_line['line'][0][0], speed_estimation_line['line'][0][1] + 35) # cv2.putText(frame, speed_estimation_line['label']+" speed", sel_label_origin, font, 1, (255, 0, 0), 2, line_type) # # ============================================================================= # ============================================================================= # if is_paused: # cv2.putText(frame, 'Pausing', (round(self.f_width/2),30), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) # ============================================================================= if roi_setting_mode and self.detector == 'yolo': get_text_box_with_background_color(frame,'Setting ROI.',(round(self.f_width/2)-150,30),font,1,(0, 0, 0),2,line_type,(255,255,255)) if roi_setting_mode_P and self.detector == 'yolo_p': get_text_box_with_background_color(frame,'Setting ROI.',(round(self.f_width/2)-150,30),font,1,(0, 0, 0),2,line_type,(255,255,255)) if line_setting_mode and self.detector == 'yolo': get_text_box_with_background_color(frame,'Setting Line.',(round(self.f_width/2)-150,30),font,1,(0, 0, 0),2,line_type,(255,255,255)) if line_setting_mode_P and self.detector == 'yolo_p': get_text_box_with_background_color(frame,'Setting Line.',(round(self.f_width/2)-150,30),font,1,(0, 0, 0),2,line_type,(255,255,255)) if vpoint_setting_mode and self.detector == 'yolo': get_text_box_with_background_color(frame,'Setting Vanishing Point.',(round(self.f_width/2)-150,30),font,1,(0, 0, 0),2,line_type,(255,255,255)) if help_img_flag and self.detector == 'yolo': cv2.putText(frame, "Press 'h' to close user manual.", (self.f_width-750,self.f_height-30), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) if help_img_flag is False and self.detector == 'yolo': cv2.putText(frame, "Press 'h' to open user manual", (self.f_width-750,self.f_height-30), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 255), 1, cv2.LINE_AA) # show detection roi if self.show_droi: frame = draw_roi(frame, self.droi,(255,255,0)) if self.detector == 'yolo_p': frame = draw_roi(frame,self.waiting_zone[0:4],(0,255,255)) if len(self.waiting_zone)==8: frame = draw_roi(frame,self.waiting_zone[-4:],(255,0,255)) if showUI: if self.detector == 'yolo': x,y = self.vanishing_point cv2.line(frame, (x+50,y+50) , (x-50,y-50) , (0, 0, 0), 2) cv2.line(frame, (x+50,y-50) , (x-50,y+50), (0, 0, 0), 2) cv2.putText(frame, 'Showing Vanishing Point.', (round(self.f_width/2)-150,30), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) # ============================================================================= # for road in self.roads: # cv2.line(frame, road['line'][0], road['line'][1], (255, 0, 0), 3) # r_label_origin = (road['line'][1][0] - 15 , road['line'][1][1] - 35) # cv2.putText(frame, road['label'] , r_label_origin, font, 1, (255, 0, 0), 2, line_type) # ============================================================================= # show counts if self.show_counts: if self.detector == 'yolo': offset = 1 for line, objects in self.counts.items(): cv2.putText(frame, line, (75, 40 * offset), font, 1, (255, 0, 0), 2, line_type) for label, count in objects.items(): offset += 1 cv2.putText(frame, "{}: {}".format(label, count), (75, 40 * offset), font, 1, (255, 0, 0), 2, line_type) offset += 2 if self.detector == 'yolo_p': if self.veh_light == 0: get_text_box_with_background_color(frame,"waiting zone have "+str(self.isWaiting)+" people.",(self.f_width - 550 , 150),font,1,(255, 255, 255),1,line_type,(0,0,0)) # adjust traffic light duration when vehicle traffic light is red and pedestrian traffic light is green. if self.detector == 'yolo' and self.veh_light == 2 and self.ped_light == 1: if len(self.blobs) == 0 and light_time < 6: self.extend_flag = True self.extend_notification = 10 print("2,1,process activated extend") if self.detector == 'yolo_p' and self.veh_light == 2 and self.ped_light == 1: if len(self.blobs) == 0 and light_time > 6: self.reduce_flag = True self.reduce_notification = 10 print("2,1,process activated reduce") if self.extend_notification != 0 : cv2.putText(frame, 'traffic light duration is extended', (10 , 450), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) self.extend_notification -= 0 if self.reduce_notification != 0 : cv2.putText(frame, 'traffic light duration is reduced', (10 , 450), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) self.reduce_notification -= 0 # adjust traffic light duration when vehicle traffic light is green and pedestrian traffic light is red. if self.detector == 'yolo' and self.veh_light == 0 and self.ped_light == 0: # print(light_time) # print(self.veh_light) # print(self.ped_light) if len(self.blobs) == 0 and light_time > 6: self.reduce_flag = True self.reduce_notification = 10 print("0,0,process activated reduce") if self.detector == 'yolo_p' and self.veh_light == 0 and self.ped_light == 0: if self.isWaiting == 0 and light_time < 6: self.extend_flag = True self.extend_notification = 10 print("0,0,process activated extend") if self.extend_notification != 0 : cv2.putText(frame, 'traffic light duration is extended', (10 , 450), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) self.extend_notification -= 1 if self.reduce_notification != 0 : cv2.putText(frame, 'traffic light duration is reduced', (10 , 450), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) self.reduce_notification -= 1 if self.jaywalker_flag: get_text_box_with_background_color(frame,"Warning: Jaywalker detected.",(250 , 100),font,2,(255, 255, 255),2,line_type,(0,0,255)) cv2.rectangle(frame, (0, 0), (self.f_width, self.f_height), (0, 0, 255), 10) if self.not_slowing_down_flag: get_text_box_with_background_color(frame,"Warning: High Speed Vehicle deteced.",(250 , 100),font,1,(255, 255, 255),2,line_type,(0,0,255)) cv2.rectangle(frame, (0, 0), (self.f_width, self.f_height), (0, 0, 255), 10) return frame