65.9K
CodeProject 正在变化。 阅读更多。
Home

AI 社交距离检测器:视频帧中的对象检测

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2020年12月7日

CPOL

2分钟阅读

viewsIcon

5557

downloadIcon

114

在本文中,我们将继续学习如何使用AI构建社交距离检测器。

你可以在这里找到配套代码。

摄像头捕捉

我首先实现了Camera类,它有助于从网络摄像头捕捉帧(参见Part_04中的camera.py)。为此,我使用了OpenCV。特别是,我使用了VideoCapture类。我获取了默认网络摄像头的引用,并将其存储在camera_capture字段中。

def __init__(self):
    # Initialize the camera capture
    try:
        self.camera_capture = opencv.VideoCapture(0)
    except expression as identifier:
        print(identifier)

要捕捉一帧,请使用VideoCapture类实例的read方法。它返回两个值:

  • status – 一个布尔变量,表示捕获的状态。
  • frame – 使用摄像头获取的实际帧。

在使用帧之前检查状态是一个好的做法。此外,在某些设备上,第一帧可能为空白。Camera类的capture_frame方法通过根据输入参数忽略第一帧来补偿。

def capture_frame(self, ignore_first_frame):
    # Get frame, ignore the first one if needed
    if(ignore_first_frame):
        self.camera_capture.read()
 
    (capture_status, current_camera_frame) = self.camera_capture.read()
    
    # Verify capture status
    if(capture_status):
        return current_camera_frame
 
    else:
        # Print error to the console
        print('Capture error')

使用Camera类的通用流程是调用初始化程序一次,然后根据需要调用capture_frame

引用先前开发的模块

为了进一步进行,我们将使用先前开发的Inference类和ImageHelper。为此,我们将使用main.py,在其中引用这些模块。这些模块的源代码包含在Part_03文件夹中,并在上一篇文章中进行了说明。

为了引用这些模块,我在main.py中添加了以下语句(假设main.py文件是从Part_04文件夹执行的):

import sys
sys.path.insert(1, '../Part_03/')
 
from inference import Inference as model
from image_helper import ImageHelper as imgHelper
 
Now, we can easily access the object detector, and perform inference (object detection), even though the source files are in a different folder:
 
# Load and prepare model
model_file_path = '../Models/01_model.tflite'
labels_file_path = '../Models/02_labels.txt'
 
# Initialize model
ai_model = model(model_file_path, labels_file_path)
 
# Perform object detection
score_threshold = 0.5
results = ai_model.detect_objects(camera_frame, score_threshold)

整合

我们只需要从摄像头捕捉帧并将其传递给AI模块。以下是完整的示例(参见main.py):

import sys
sys.path.insert(1, '../Part_03/')
 
from inference import Inference as model
from image_helper import ImageHelper as imgHelper
 
from camera import Camera as camera
 
if __name__ == "__main__": 
    # Load and prepare model
    model_file_path = '../Models/01_model.tflite'
    labels_file_path = '../Models/02_labels.txt'
 
    # Initialize model
    ai_model = model(model_file_path, labels_file_path)
 
    # Initialize camera
    camera_capture = camera()
 
    # Capture frame and perform inference
    camera_frame = camera_capture.capture_frame(False)
        
    score_threshold = 0.5
    results = ai_model.detect_objects(camera_frame, score_threshold)
 
    # Display results
    imgHelper.display_image_with_detected_objects(camera_frame, results)

运行上述代码后,你将获得介绍中显示的结果。

总结

我们开发了一个Python控制台应用程序,它对来自网络摄像头的视频序列中的对象进行检测。虽然这是一个单帧推理,但你可以通过循环捕捉和调用推理来扩展示例,连续显示视频流,并根据需要调用推理(例如,通过按键盘上的键)。我们将在下一篇文章中对来自测试数据集的帧进行对象检测,包括存储在视频文件中的视频序列。

© . All rights reserved.