1 year ago

#369233

test-img

user18476419

How to update a constant csv file with real-time detection data?

I am trying to get the id number of a detected object into a csv file and because it is in real-time it will update the file with the id of the new detected object.

But I can't get the correct str value to be detected because if I put 'id' or 'label' or 'name' it just prints that and nothing else.

This is the code for the label map and what I want to be detected.

labels = [{'name':'nbroken', 'id':1}, {'name':'broken', 'id':2}]

with open(files['LABELMAP'], 'w') as f:
    for label in labels:
        f.write('item { \n')
        f.write('\tname:\'{}\'\n'.format(label['name']))
        f.write('\tid:{}\n'.format(label['id']))
        f.write('}\n')

This is the real-time detection code which does not update and does not print the id wanted. Does anyone know how I can fix this?

f = open("test.txt", "a+")
a=str('id')
f.write(a)
f.write('\n')
f.flush()
f.close()
cap = cv2.VideoCapture(1)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

while cap.isOpened(): 
    ret, frame = cap.read()
    image_np = np.array(frame)
    
    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    detections = detect_fn(input_tensor)
    
    num_detections = int(detections.pop('num_detections'))
    detections = {key: value[0, :num_detections].numpy()
                  for key, value in detections.items()}
    detections['num_detections'] = num_detections

    # detection_classes should be ints.
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

    label_id_offset = 1
    image_np_with_detections = image_np.copy()
    
    #for box, class, score in zip(detections['detection_boxes'], detections['detection_classes']+label_id_offset, detections['detection_scores']:
    #f.write(f"{time.time}, {box}, {class}, {labels[class]['name'])
    #f.flush()

    viz_utils.visualize_boxes_and_labels_on_image_array(
                image_np_with_detections,
                detections['detection_boxes'],
                detections['detection_classes']+label_id_offset,
                detections['detection_scores'],
                category_index,
                use_normalized_coordinates=True,
                max_boxes_to_draw=5,
                min_score_thresh=.8,
                agnostic_mode=False)

    cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))
    
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
#f.close()        

python

real-time

object-detection

object-detection-api

real-time-data

0 Answers

Your Answer

Accepted video resources