使用CodeProject.AI Server进行视频处理

在本文中,将探讨如何使用CodeProject.AI ServerPythonOpenCV库来处理视频文件。将通过一个简单的示例,展示如何加载视频文件,运行对象检测,并生成包含对象和时间戳的文件,同时在视频上叠加检测框。

设置环境

将使用YOLOv5 6.2对象检测模块在CodeProject.AI Server中进行操作。这个模块在性能上表现不错,并且它运行在共享的Python虚拟环境中,这使得可以直接使用它而不需要进行繁琐的设置步骤。

代码实现

以下是使用CodeProject.AI Server处理视频文件的代码示例。将打开一个视频文件,将其发送到CodeProject.AI Server进行对象检测,并在视频上显示检测框。

import cv2 import imutils from imutils.video import FileVideoStream from PIL import Image, ImageDraw, ImageFont import io import requests import numpy as np def do_detection(image, log_file): buf = io.BytesIO() image.save(buf, format='PNG') buf.seek(0) with requests.Session() as session: response = session.post(server_url + "vision/detection", files={"image": ('image.png', buf, 'image/png')}, data={"min_confidence": 0.5}).json() predictions = None if response is not None and "predictions" in response: predictions = response["predictions"] if predictions is not None: font = ImageFont.truetype("Arial.ttf", font_size) draw = ImageDraw.Draw(image) for object in predictions: label = object["label"] conf = object["confidence"] y_max = int(object["y_max"]) y_min = int(object["y_min"]) x_max = int(object["x_max"]) x_min = int(object["x_min"]) if y_max < y_min: temp = y_max y_max = y_min y_min = temp if x_max < x_min: temp = x_max x_max = x_min x_min = temp draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="red", width=line_width) draw.text((x_min + padding, y_min - padding - font_size), f"{label} {round(conf*100.0,0)}%", font=font) log_file.write(f"{object_info}: ({x_min}, {y_min}), ({x_max}, {y_max})\n") return image vs = FileVideoStream(file_path).start() with open("results.txt", 'w') as log_file: while True: if not vs.more(): break frame = vs.read() if frame is None: break image = Image.fromarray(frame) image = do_detection(image, log_file) frame = np.asarray(image) if frame is not None: frame = imutils.resize(frame, width=640) cv2.imshow("Movie File", frame) vs.stop() cv2.destroyAllWindows()
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485