How to perform object counting using a built object detection model?

Viewed 50

im a beginner in tensorflow and computer vision things. I have followed the tutorial from the following github https://github.com/nicknochnack/TFODCourse to create an object detection model. From here, I wanted to create an object counting model too using the following github https://github.com/ahmetozlu/tensorflow_object_counting_api. However, I couln't quite understand how to integrate the previously trained model and use it on this https://github.com/ahmetozlu/tensorflow_object_counting_api. Does anybody understand how to perform so?

I understand that this has to do with the model ssd that I have used since in the trained one, I already have checkpoints.

def cumulative_object_counting_y_axis(input_video, detection_graph, category_index, is_color_recognition_enabled, roi, deviation, custom_object_name, targeted_objects=None):
    total_passed_objects = 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_objects = 0
    color = "waiting..."
    with detection_graph.as_default():
      with tf.compat.v1.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_result = vis_util.visualize_boxes_and_labels_on_image_array_y_axis(cap.get(1),
                                                                                                         input_frame,
                                                                                                         is_color_recognition_enabled,
                                                                                                         np.squeeze(boxes),
                                                                                                         np.squeeze(classes).astype(np.int32),
                                                                                                         np.squeeze(scores),
                                                                                                         category_index,
                                                                                                         targeted_objects = targeted_objects,
                                                                                                         y_reference = roi,
                                                                                                         deviation = deviation,
                                                                                                         use_normalized_coordinates=True,
                                                                                                         line_thickness=4)

            # when the object 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_objects = total_passed_objects + counter

            # insert information text to video frame
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(
                input_frame,
                'Detected ' + custom_object_name + ': ' + str(total_passed_objects),
                (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()
0 Answers
Related