def cumulative_object_custom_counting_y_axis(input_video, detection_graph, category_index, is_color_recognition_enabled, fps, width, height, roi, deviation): total_passed_vehicle = 0 #initialize .csv with open('object_counting_report.csv', 'w') as f: writer = csv.writer(f) csv_line = "Object Type, Object Color, Object Movement Direction, Object Speed (km/h)" writer.writerows([csv_line.split(',')]) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter('the_output.avi', fourcc, fps, (width, height)) # input video cap = cv2.VideoCapture(input_video) total_car = 0 total_pedesteriane: int = 0 total_bicycle: int = 0 total_truck: int = 0 total_motorcycle: int = 0 total_others: int = 0 total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') # for all the frames that are extracted from input video while (cap.isOpened()): ret, frame = cap.read() if not ret: print("end of the video file...") break input_frame = frame # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis( cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference=roi, deviation=deviation, use_normalized_coordinates=True, line_thickness=4) # when the vehicle passed over line and counted, make the color of ROI line green if counter == 1: cv2.line(input_frame, (0, roi), (width, roi), (0, 0xFF, 0), 1) if (csv_line.__contains__('car')): total_car = total_car + counter database.mysqlInsert("Car", counter) elif (csv_line.__contains__('person')): total_pedesteriane = total_pedesteriane + counter database.mysqlInsert("Person", counter) elif (csv_line.__contains__('bicycle')): total_bicycle = total_bicycle + counter database.mysqlInsert("Bicycle", counter) elif (csv_line.__contains__('truck')): total_truck = total_truck + counter database.mysqlInsert("Truck", counter) elif (csv_line.__contains__('motorcycle')): total_motorcycle = total_motorcycle + counter database.mysqlInsert("MoterCycle", counter) else: total_others = total_others + counter database.mysqlInsert("Other", counter) else: cv2.line(input_frame, (0, roi), (width, roi), (0, 0, 0xFF), 1) total_passed_vehicle = total_passed_vehicle + counter # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'All: ' + str(total_passed_vehicle), (10, 30), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'MotorCycle: ' + str(total_motorcycle), (10, 130), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'Truck: ' + str(total_truck), (10, 110), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'bicycle: ' + str(total_bicycle), (10, 90), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'Car: ' + str(total_car), (10, 70), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'Person: ' + str(total_pedesteriane), (10, 50), font, 0.6, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) output_movie.write(input_frame) # print ("writing frame") cv2.imshow('object counting', input_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break if (csv_line != "not_available"): with open('traffic_measurement.csv', 'a') as f: writer = csv.writer(f) size, direction = csv_line.split(',') writer.writerows([csv_line.split(',')]) cap.release() cv2.destroyAllWindows()
def cumulative_object_counting_y_axis(input_video, detection_graph, category_index, is_color_recognition_enabled, roi, deviation): total_passed_vehicle = 0 # input video cap = cv2.VideoCapture(input_video) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(cap.get(cv2.CAP_PROP_FPS)) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter('the_output.avi', fourcc, fps, (width, height)) total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # for all the frames that are extracted from input video while(cap.isOpened()): ret, frame = cap.read() if not ret: print("end of the video file...") break input_frame = frame # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis(cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference = roi, deviation = deviation, use_normalized_coordinates=True, line_thickness=4) # when the vehicle passed over line and counted, make the color of ROI line green if counter == 1: cv2.line(input_frame, (0, roi), (width, roi), (0, 0xFF, 0), 5) else: cv2.line(input_frame, (0, roi), (width, roi), (0, 0, 0xFF), 5) total_passed_vehicle = total_passed_vehicle + counter # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Detected Vehicles: ' + str(total_passed_vehicle), (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'ROI Line', (545, roi-10), font, 0.6, (0, 0, 0xFF), 2, cv2.LINE_AA, ) output_movie.write(input_frame) print ("writing frame") #cv2.imshow('object counting',input_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def cumulative_object_counting_y_axis(input_video, detection_graph, category_index, is_color_recognition_enabled, roi, deviation): total_passed_vehicle = 0 # input video cap = cv2.VideoCapture(input_video) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(cap.get(cv2.CAP_PROP_FPS)) ## print(width) ## print(height) ## print(fps) cap = cv2.VideoCapture('traffic_video.mp4') total_passed_vehicle = 0 total_passed_vehicle1 = 0 total_passed_vehicle2 = 0 total_passed_vehicle3 = 0 total_passed_vehicle4 = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') #print(num_detections) # for all the frames that are extracted from input video while(cap.isOpened()): ret, frame = cap.read() if not ret: print("end of the video file...") break ## upper_left = (50,120) ## bottom_right = (1000,650) input_frame = frame ## r = cv2.rectangle(input_frame, upper_left, bottom_right, (255,0,0), 5) ## rect_img = input_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]] #roi_color = input_frame #input_frame[100:700 , 700:200] = [255,255,255] #roit = cv2.rectangle(roi_color, (100,650), (700,200), (255,0,0),2) #print(input_frame) # cv2.resize(input_frame,(800,450)) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode,counter1, counter2, counter3, counter4 = vis_util.visualize_boxes_and_labels_on_image_array_y_axis(cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference = roi, deviation = deviation, use_normalized_coordinates=True, line_thickness=4) ## for i,b in enumerate(boxes[0]): ## # car bus truck ## if classes[0][i] == 1 or classes[0][i] == 2 or classes[0][i] == 4: ## if scores[0][i] >= 0.5: ## mid_x = (boxes[0][i][1]+boxes[0][i][3])/2 ## mid_y = (boxes[0][i][0]+boxes[0][i][2])/2 ## apx_distance = round(((1 - (boxes[0][i][3] - boxes[0][i][1]))**4),1) ## cv2.putText(input_frame, '{}'.format(apx_distance), (int(mid_x*640),int(mid_y*360)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2) ## ## if apx_distance <=0.7: ## if mid_x > 0.3 and mid_x < 0.7: ## cv2.putText(input_frame, 'WARNING!!!', (int(mid_x*640)-50,int(mid_y*360)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 2) ## ## ## # when the vehicle passed over line and counted, make the color of ROI line green if counter == 1: cv2.line(input_frame, (0, roi), (width, roi), (0, 0xFF, 0), 3) else: cv2.line(input_frame, (0, roi), (width, roi), (0, 0, 0xFF), 3) total_passed_vehicle = total_passed_vehicle + counter total_passed_vehicle1 = total_passed_vehicle1 + counter1 total_passed_vehicle2 = total_passed_vehicle2 + counter2 total_passed_vehicle3 = total_passed_vehicle3 + counter3 total_passed_vehicle4 = total_passed_vehicle4 + counter4 a = total_passed_vehicle b = total_passed_vehicle1 c = total_passed_vehicle2 d = total_passed_vehicle3 e = total_passed_vehicle4 conn = sqlite3.connect('traffic.db') c = conn.cursor() def create_table(): c.execute("CREATE TABLE IF NOT EXISTS detect(Detected INT, Cars INT, Bikes INT,Rikshaws INT, Bus INT )") def data_entry(): c.execute("INSERT INTO detect (Detected, Cars, Bikes, Rikshaws, Bus) VALUES (?, ?, ?, ?, ?)", (total_passed_vehicle,total_passed_vehicle1, total_passed_vehicle2, total_passed_vehicle3,total_passed_vehicle4)) conn.commit() c.close() conn.close() create_table() data_entry() # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Detected Vehicles:' + str(total_passed_vehicle), (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Cars:' + str(total_passed_vehicle1), (270, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Bikes:' + str(total_passed_vehicle2), (375, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Rikshaws:' + str(total_passed_vehicle3), (480, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Bus:' + str(total_passed_vehicle4), (650, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) #*********************************************# ## ## # insert information text to video frame ## font = cv2.FONT_HERSHEY_SIMPLEX ## cv2.putText( ## input_frame, ## 'Detected Vehicles: ' + str(total_passed_vehicle), ## (10, 35), ## font, ## 0.8, ## (0, 0xFF, 0xFF), ## 2, ## cv2.FONT_HERSHEY_SIMPLEX, ## ) ## cv2.putText( input_frame, 'ROI Line', (545, roi-10), font, 0.6, (0, 0, 0xFF), 2, cv2.LINE_AA, ) #output_movie.write(input_frame) #print ("writing frame") cv2.imshow('object counting',input_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() return(width, height, fps)
def single_image_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled): total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') input_frame = cv2.imread(input_video) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis( cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference=roi, deviation=deviation, use_normalized_coordinates=True, line_thickness=1) if (len(counting_mode) == 0): cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) cv2.imshow('tensorflow_object counting_api', input_frame) cv2.waitKey(0) return counting_mode
def couting_parcel_passed_line(input_video, detection_graph, category_index, is_color_recognition_enabled, roi, deviation, isShowFrame, isWriteVideoOutput): total_parcel = 0 # input video cap = cv2.VideoCapture(input_video + ".mp4") height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(cap.get(cv2.CAP_PROP_FPS)) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter(input_video + '-writed.mp4', fourcc, fps, (width, height)) total_parcel = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True frame_counted = 0 with detection_graph.as_default(): #with tf.device("/gpu:0"): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') # for all the frames that are extracted from input video while (cap.isOpened()): ret, frame = cap.read() print('Start process frame: ' + str(frame_counted) + '...') if not ret: print("end of the video file...") break input_frame = frame # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis( cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference=roi, deviation=deviation, use_normalized_coordinates=True, line_thickness=1) # when the vehicle passed over line and counted, make the color of ROI line green print("Count in frame: " + str(counter)) if counter == 1: cv2.line(input_frame, (530, roi), (1010, roi), (0, 0xFF, 0), 2, 8) else: cv2.line(input_frame, (530, roi), (1010, roi), (0, 0, 0xFF), 2, 8) total_parcel = total_parcel + counter ''' ******************************************************************************************************** -------------------------------------------------------------- THIS INPUT THE NUMBER OF PARCELS WHICH HAS BEEN SORTED # insert information text to video frame -------------------------------------------------------------- ******************************************************************************************************** ''' cv2.putText( input_frame, 'Detected Parcels: ' + str(total_parcel), (800, 100), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) ''' font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, str(total_parcel), (550, 640), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, str(total_parcel), (576, 489), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, str(total_parcel), (595, 373), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, str(total_parcel), (612, 281), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, str(total_parcel), (625, 211), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, '0', (980, 630), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, '0', (952, 491), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, '0', (922, 370), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, '0', (890, 281), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, '0', (870, 209), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) ''' if isShowFrame: # Display the resulting frame cv2.imshow('Vtp-Tracking', frame) if isWriteVideoOutput: #write result output_movie.write(input_frame) #Write log frame_counted += 1 print("------ End process frame: " + str(frame_counted)) # Press Q on keyboard to exit if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def custom_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled, fps, width, height): # initialize .csv with open('object_counting_report.csv', 'w') as f: writer = csv.writer(f) csv_line = "Object Type, Object Color, Object Movement Direction, Object Speed (km/h)" writer.writerows([csv_line.split(',')]) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter('the_output.avi', fourcc, fps, (width, height)) # input video cap = cv2.VideoCapture(input_video) objectType = "" total_vehicle = 0 total_pedesteriane: int = 0 total_others: int = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True height = 0 width = 0 with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') # for all the frames that are extracted from input video while (cap.isOpened()): ret, frame = cap.read() if not ret: print("end of the video file...") break input_frame = frame # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. # counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis(cap.get(1), # input_frame, # 1, # is_color_recognition_enabled, # np.squeeze(boxes), # np.squeeze(classes).astype(np.int32), # np.squeeze(scores), # category_index, # # x_reference=200, # y_reference = 200, # deviation=3, # use_normalized_coordinates=True, # line_thickness=4) counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis( cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference=200, deviation=3, use_normalized_coordinates=True, line_thickness=4) if (len(counting_mode) == 0): cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) # # splitct= counting_mode.split(',') for x in counting_mode.split(','): print(x) counts = 0 objectType = x objectsplit = objectType.split(':') counts = int(objectsplit[objectsplit.__len__() - 1]) if (objectType.__contains__('car')): total_vehicle = total_vehicle + counts elif (objectType.__contains__('person')): total_pedesteriane = total_pedesteriane + counts else: total_others = total_others + counts # size=len(splitct) # # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Detected Vehicle: ' + str(total_vehicle), (10, 105), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'Detected Person: ' + str(total_pedesteriane), (10, 70), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'X Line', (545, 200 - 10), font, 0.6, (0xFF, 0, 0), 2, cv2.LINE_AA, ) cv2.putText( input_frame, 'Y Line', (545, 200 - 10), font, 0.6, (0, 0xFF, 0), 2, cv2.LINE_AA, ) output_movie.write(input_frame) print("writing frame") cv2.imshow('object counting', input_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break if (csv_line != "not_available"): with open('traffic_measurement.csv', 'a') as f: writer = csv.writer(f) size, direction = csv_line.split(',') writer.writerows([csv_line.split(',')]) cap.release() cv2.destroyAllWindows()
def cumucount(): input_video = "tiv.mp4" # input_video = "http://*****:*****@192.168.0.105:80/1" # By default I use an "SSD with Mobilenet" model here. See the detection model zoo (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies. detection_graph, category_index = backbone.set_model( 'ssd_mobilenet_v1_coco_2017_11_17') targeted_objects = "person" fps = 24 # change it with your input video fps width = 640 # change it with your input video width height = 480 # change it with your input vide height is_color_recognition_enabled = 0 # set it to 1 for enabling the color prediction for the detected objects roi = 200 # roi line position deviation = 5 # the constant that represents the object counting area total_passed_vehicle = 0 fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter('the_output.avi', fourcc, fps, (width, height)) # input video cap = cv2.VideoCapture(input_video) cam_resolution = RESOLUTION_VGA model_detector = FaceDetectorModels.HAARCASCADE model_recognizer = FaceEncoderModels.LBPH try: # Initialize face detection face_detector = FaceDetector(model=model_detector, path=INPUT_DIR_MODEL_DETECTION) # Initialize face recognizer face_encoder = FaceEncoder(model=model_recognizer, path=INPUT_DIR_MODEL_ENCODING, path_training=INPUT_DIR_MODEL_TRAINING, training=False) except: face_encoder = None print("Warning, check if models and trained dataset models exists!") face_id, confidence = (None, 0) total_passed_vehicle = 0 counting_mode = "..." width_heigh_taken = True with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') # for all the frames that are extracted from input video while (cap.isOpened()): ret, frame = cap.read() if not ret: print("end of the video file...") break input_frame = frame # Detect and identify faces in the frame faces = face_detector.detect(input_frame) for (index, face) in enumerate(faces): (x, y, w, h) = face # Identify face based on trained dataset (note: should run facial_recognition_training.py) if face_encoder is not None: face_id, confidence = face_encoder.identify( input_frame, (x, y, w, h)) # Set text and bounding box on face label_face(input_frame, (x, y, w, h), face_id, confidence) # Process 1 face only #break # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis( cap.get(1), input_frame, 2, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, targeted_objects="person", y_reference=roi, deviation=deviation, use_normalized_coordinates=True, line_thickness=4) # when the vehicle passed over line and counted, make the color of ROI line green if counter == 1: cv2.line(input_frame, (0, roi), (width, roi), (0, 0xFF, 0), 5) else: cv2.line(input_frame, (0, roi), (width, roi), (0, 0, 0xFF), 5) total_passed_vehicle = total_passed_vehicle + counter # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Detected: ' + str(total_passed_vehicle), (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'ROI Line', (545, roi - 10), font, 0.6, (0, 0, 0xFF), 2, cv2.LINE_AA, ) output_movie.write(input_frame) #print ("writing frame") #cv2.imshow('object counting',input_frame) #if cv2.waitKey(1) & 0xFF == ord('q'): #break # Display updated frame to web app yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame)[1].tobytes() + b'\r\n\r\n') cap.release() cv2.destroyAllWindows()