首页 > 解决方案 > Raspberry pi - Tensorflow 如何实时检测人员和计数?

问题描述

我尝试用覆盆子 pi 制作一个相机,它可以计算并在 Firebase 上发送碎片中的人数。

我按照https://github.com/EdjeElectronics/TensorFlow-Object-Detection-on-the-Raspberry-Pi的教程进行操作, 但我不能将检测仅限于人并计算房间内要检测的人数。我可以发送到火力基地,但我没有合适的人数。

我在 stackeoverflow 上尝试了几个答案,但它不能作为如何在 Tensorflow 对象检测 API 中计算对象, 因为它们不存在仅计算人数的问题。

我怎样才能只计算房间里的人?这是我的代码:

    for frame1 in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
    totalcount=0
    t1 = cv2.getTickCount()

    # Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    frame = np.copy(frame1.array)
    frame.setflags(write=1)
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame_expanded = np.expand_dims(frame_rgb, axis=0)

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: frame_expanded})
    boxes = np.squeeze(boxes)
    scores = np.squeeze(scores)
    classes = np.squeeze(classes)


    indices = np.argwhere(classes == 1)
    boxes = np.squeeze(boxes[indices])
    scores = np.squeeze(scores[indices])
    classes = np.squeeze(classes[indices])



    # Draw the results of the detection (aka 'visulaize the results')
    vis_util.visualize_boxes_and_labels_on_image_array(
        frame,
        boxes,
        classes,
        scores,
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.70)

    cv2.putText(frame,"FPS: {0:.2f}".format(frame_rate_calc),(30,50),font,1,(255,255,0),2,cv2.LINE_AA)
    #print(category_index)

    try:
        app = firebase_admin.get_app()
    except ValueError as e:
        cred = credentials.Certificate("./ServiceAccountKey.json")
        app = firebase_admin.initialize_app(cred)

    store = firestore.client()
    #doc_ref = store.collection(u'users').limit(2)

    print(len(boxes.shape))

    totalcount= boxes.shape[1]

    #x = re.search('[\w]', str(totalcount))

    #totalcount = x.group()
    print("Il y'a {0} personne dans la salle".format(totalcount))            
    # All the results have been drawn on the frame, so it's time to display it.
    store.collection(u'Piece').document(u'Cuisine').set({u'nbrPers': int(totalcount)})

    cv2.putText(frame,"NbrPerson"+str(totalcount) ,(50, 45),font,0.8,(0, 0xFF, 0xFF),2,cv2.FONT_HERSHEY_SIMPLEX,)

    cv2.imshow('Object detector', frame)

    t2 = cv2.getTickCount()
    time1 = (t2-t1)/freq
    frame_rate_calc = 1/time1

    # Press 'q' to quit
    if cv2.waitKey(1) == ord('q'):
        break

    rawCapture.truncate(0)

camera.close()

标签: python-3.xopencvtensorflowraspberry-pi

解决方案


推荐阅读