一种解决实时视频预测的延迟方案

最近在做实时视频预测火焰时,总是会遇到延迟问题,轻则几秒,重则十几秒,几十秒,很难接受
下面一种方案可以把延迟控制在1秒内

总体思想:采用kafka

kafka安装参考 安装kafka
生产者获取视频流得到每一帧,调用算法模型处理,处理后的帧放入 kafka ,
消费者订阅 kafka 的消息播放视频。

生产者代码如下 kafka_producer.py

# -*- coding: utf-8 -*-
from kafka import KafkaProducer
from imutils.video import VideoStream
import requests
import imutils
import numpy as np
import time
import cv2
from PIL import Image
from io import BytesIO
import base64
import json


def publish_video(server, topic, rtsp, api_url):
   producer = KafkaProducer(bootstrap_servers=server)
   vs = VideoStream(src=rtsp).start()
   time.sleep(2.0)

   print("publishing video...")

   while True:
      frame = vs.read()
      frame = process(frame, api_url)
      frame = imutils.resize(frame, width=400)
      # send to kafka topic
      producer.send(topic, frame.tobytes())

   vs.stop()


def process(frame, api_url):
   img = Image.fromarray(frame)
   output_buffer = BytesIO()  # 创建一个BytesIO
   img.save(output_buffer, format='JPEG')  # 写入output_buffer
   byte_data = output_buffer.getvalue()  # 在内存中读取
   image_base64 = base64.b64encode(byte_data).decode()  # 转为BASE64
   data = json.dumps(
      {"image": image_base64, "visual_result": "True", "username": "xsrt", "password": "dGVzdC1wd2QxMjM="})
   headers = {"Content-Type": "application/json"}
   response = requests.post(api_url, data=data, headers=headers)
   result = response.text
   result = json.loads(result)
   try:
      base64_img = result["base64"]
      process_img = base64.b64decode(base64_img)
      img = cv2.imdecode(np.frombuffer(process_img, np.uint8), cv2.IMREAD_COLOR)
      img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
   except:
      img = frame

   return img


def process_list(result):
   for i in result:
      result_i = i
      result_i_bbox = result_i["bbox"]
      for i in range(len(result_i_bbox)):
         result_i_bbox[i] = result_i_bbox[i] * 2
   return result


if __name__ == '__main__':
   server = '192.168.20.99:9092'
   topic = 'run'
   # rtsp = 'rtsp://admin:[email protected]:554/h264/ch1/main/av_stream'
   rtsp = 0
   api_url = 'http://192.168.20.99:8191/fire/json'
   publish_video(server, topic, rtsp, api_url)

消费者代码如下 kafka_consumer.py

from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import time
import cv2
from kafka import KafkaConsumer
import sys

def showCam(server, topic, window_name):

    consumer = KafkaConsumer(
        topic,
        bootstrap_servers=[server])

    fps = FPS().start()

    for msg in consumer:
        # print(msg)
        decoded = np.frombuffer(msg.value, np.uint8)
        # print(decoded.size)
        # decoded = decoded.reshape(225, 400, 3)
        decoded = decoded.reshape((int)(decoded.size / (400 * 3)), 400, 3)
        cv2.namedWindow(window_name, flags=cv2.WINDOW_FREERATIO)
        cv2.imshow(window_name, decoded)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
        fps.update()

    fps.stop()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    server = '192.168.20.99:9092'
    topic = 'run'
    window_name = 'fire'
    showCam(server, topic, window_name)

进一步优化

kafka_producer.py

# -*- coding: utf-8 -*-
from kafka import KafkaProducer
from imutils.video import VideoStream
import requests
import imutils
import numpy as np
import time
import cv2
from PIL import Image
from io import BytesIO
import base64
import json


def publish_video(server, topic, rtsp, api_url):
   producer = KafkaProducer(bootstrap_servers=server)
   vs = VideoStream(src=rtsp).start()
   time.sleep(2.0)

   print("publishing video...")

   i = 0
   a = 5
   while True:
      i = i +1
      if i % a == 0:
         frame = vs.read()
         if q.qsize() > 1:
            q.get()
         else:
            q.put(frame)
            # frame = process(frame)
            # frame = imutils.resize(frame, width=400)
            frame = q.get()
            if frame is not None:
               frame = imutils.resize(frame, width=400)
               # frame = detection(frame, 'pretrained.prototxt.txt', 'pretrained.caffemodel')
               frame = process(frame)
               # send to kafka topic
               producer.send(topic, frame.tobytes())

   vs.stop()


def process(frame, api_url):
   img = Image.fromarray(frame)
   output_buffer = BytesIO()  # 创建一个BytesIO
   img.save(output_buffer, format='JPEG')  # 写入output_buffer
   byte_data = output_buffer.getvalue()  # 在内存中读取
   image_base64 = base64.b64encode(byte_data).decode()  # 转为BASE64
   data = json.dumps(
      {"image": image_base64, "visual_result": "True", "username": "xsrt", "password": "dGVzdC1wd2QxMjM="})
   headers = {"Content-Type": "application/json"}
   response = requests.post(api_url, data=data, headers=headers)
   result = response.text
   result = json.loads(result)
   try:
      base64_img = result["base64"]
      process_img = base64.b64decode(base64_img)
      img = cv2.imdecode(np.frombuffer(process_img, np.uint8), cv2.IMREAD_COLOR)
      img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
   except:
      img = frame

   return img


def process_list(result):
   for i in result:
      result_i = i
      result_i_bbox = result_i["bbox"]
      for i in range(len(result_i_bbox)):
         result_i_bbox[i] = result_i_bbox[i] * 2
   return result


if __name__ == '__main__':
   server = '192.168.20.99:9092'
   topic = 'run'
   # rtsp = 'rtsp://admin:[email protected]:554/h264/ch1/main/av_stream'
   rtsp = 0
   api_url = 'http://192.168.20.99:8191/fire/json'
   publish_video(server, topic, rtsp, api_url)

还可以再优化,参考下面的方案
读取多个(海康\大华)网络摄像头的视频流 (使用opencv-python),解决实时读取延迟问题

猜你喜欢

转载自blog.csdn.net/qq122716072/article/details/109577366