如何使用TensorFlow和Webcam实现实时对象检测?[已关闭]

disbfnqx  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(118)

已关闭。此问题需要更多focused。它目前不接受回答。
**希望改进此问题?**更新问题,使其仅针对editing this post的一个问题。

3天前关闭。
Improve this question
我想使用TensorFlow和网络摄像头源创建一个实时对象检测系统。该系统应该能够检测多个对象,并在实时视频中显示它们周围的边界框。
我尝试使用TensorFlow的对象检测API,但在将其与网络摄像头提要集成时遇到了困难。我希望在网络摄像头的馈送中看到实时对象检测结果,检测到的对象周围有边界框。

bq3bfh9z

bq3bfh9z1#

1.安装依赖项:确保已安装必要的库。您将需要TensorFlow、OpenCV和其他所需的库。您可以使用pip安装它们:

pip install tensorflow opencv-python

字符串
1.设置TensorFlow的对象检测API:按照TensorFlow的Object Detection API的官方安装说明进行操作,可以在TensorFlow Model Garden存储库中找到:https://github.com/tensorflow/models/tree/master/research/object_detection
1.准备对象检测模型:从TensorFlow Model Zoo中选择预训练的对象检测模型或训练您自己的模型。下载模型文件并将其放在文件夹中。确保模型的saved_model目录包含推理图。
1.创建对象检测脚本:创建一个Python脚本来处理对象检测并在网络摄像头提要上显示结果。
以下是使用TensorFlow的Object Detection API和OpenCV进行实时对象检测的示例脚本:

import cv2
import numpy as np
import tensorflow as tf

# Path to the saved_model directory of your trained object detection model
MODEL_PATH = "path/to/your/saved_model"

# Load the TensorFlow model
detect_fn = tf.saved_model.load(MODEL_PATH)

# Label map (replace this with your own label map if needed)
LABEL_MAP = {1: 'person', 2: 'car', 3: 'cat', ...}

def detect_objects(image_np):
    # Convert the input image to a tensor
    input_tensor = tf.convert_to_tensor(image_np)
    input_tensor = input_tensor[tf.newaxis, ...]

    # Perform inference on the input tensor
    detections = detect_fn(input_tensor)

    # Extract useful information from the detections
    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

    # Filter detections that have confidence scores above a certain threshold (e.g., 0.5)
    detection_threshold = 0.5
    detected_classes = detections['detection_classes']
    detected_scores = detections['detection_scores']
    valid_detections = detected_classes[detected_scores > detection_threshold]
    valid_scores = detected_scores[detected_scores > detection_threshold]
    valid_boxes = detections['detection_boxes'][detected_scores > detection_threshold]

    return valid_detections, valid_scores, valid_boxes

def draw_bounding_boxes(image_np, detections, scores, boxes):
    im_height, im_width, _ = image_np.shape

    for i in range(len(detections)):
        class_id = int(detections[i])
        class_name = LABEL_MAP[class_id]
        score = scores[i]

        ymin, xmin, ymax, xmax = boxes[i]
        left = int(xmin * im_width)
        top = int(ymin * im_height)
        right = int(xmax * im_width)
        bottom = int(ymax * im_height)

        cv2.rectangle(image_np, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(image_np, f'{class_name} {score:.2f}', (left, top - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)

    return image_np

# Open the webcam feed
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    if not ret:
        break

    # Perform object detection on the frame
    detections, scores, boxes = detect_objects(frame)
    frame = draw_bounding_boxes(frame, detections, scores, boxes)

    # Display the frame with bounding boxes
    cv2.imshow('Object Detection', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the webcam and close the OpenCV window
cap.release()
cv2.destroyAllWindows()

相关问题