openCV 多线程调用多个海康威视摄像头(避免画面不同步),进行人脸识别

1、创建多个线程来拉取不同的视频流,不使用多线程的话,所有的视频流就在排一个队,会出现画面不同步的情况

代码:

from threading import Thread
import cv2


#视频流拉取类
class vStream:
    def __init__(self,src):
        self.capture = cv2.VideoCapture(src)
        self.thread = Thread(target=(self.update),args=())
        self.thread.daemon = True
        self.thread.start()
    def update(self):
        while True:
            _,self.frame = self.capture.read()
    def getFrame(self):
        return self.frame


#我的网络摄像头
cam1 = vStream('rtsp://admin:qwertyuiop123@192.168.31.103/Streaming/Channels/2')
cam2 = vStream('rtsp://admin:JGSYS123@192.168.31.123/Streaming/Channels/2')

while True:
    try:
        myframe1 = cam1.getFrame()
        myframe2 = cam2.getFrame()
        cv2.imshow('webCam1',myframe1)
        cv2.imshow('webCam2',myframe2)
    except:
        print('frame not available')

    if cv2.waitKey(1) ==ord('q'):
        cam1.capture.release()
        cam2.capture.release()
        cv2.destroyAllWindows()
        exit(1)
        break

2、使用numpy将多个画面放在同一个frame中,图像就是矩阵,numpy是用来处理矩阵的类

代码:

import time
from threading import Thread
import cv2
import numpy as np


#视频流拉取类
class vStream:
    def __init__(self,src,width,height):
        #frame 初始尺寸
        self.width = width
        self.height = height
        self.capture = cv2.VideoCapture(src)
        self.thread = Thread(target=(self.update),args=())
        self.thread.daemon = True
        self.thread.start()
    def update(self):
        while True:
            _,self.frame = self.capture.read()
            self.frame2 = cv2.resize(self.frame,(self.width,self.height))

    def getFrame(self):
        return self.frame2

ip1 = 'rtsp://admin:qwertyuiop123@192.168.31.103/Streaming/Channels/2'
ip2 = 'rtsp://admin:JGSYS123@192.168.31.123/Streaming/Channels/2'

disW = 640
disH = 360
cam1 = vStream(ip1,disW,disH)
cam2 = vStream(ip2,disW,disH)
#显示帧率
font = cv2.FONT_HERSHEY_SIMPLEX
startTime = time.time()
dtav = 1#没啥意义,只是为了第一次进入循环时没有dtav避免错误

while True:
    try:
        myframe1 = cam1.getFrame()
        myframe2 = cam2.getFrame()
        #将myframe1,myframe2 放在同一个画面
        frameCombine = np.hstack((myframe1,myframe2))

        #显示帧率
        dt = time.time() - startTime
        startTime = time.time()
        # 低通滤波,避免fps误差太大
        dtav = .90 * dtav + .1 * dt
        fps = 1 / dtav
        fps = round(fps, 2)
        # print('fps is:',round(dtav,1))
        cv2.rectangle(frameCombine, (0, 0), (100, 40), (0, 0, 255), -1)
        cv2.putText(frameCombine, str(round(fps, 1)) + 'fps', (0, 25), font, .75, (0, 255, 255, 2))
        cv2.imshow('combine frame', frameCombine)

    except:
        print('frame not available')

    keyCode = cv2.waitKey(1) & 0xFF
    if keyCode == 27:  # ESC键退出
        cam1.capture.release()
        cam2.capture.release()
        cv2.destroyAllWindows()
        exit(1)
        break

 进行人脸识别,网不好帧率太低

import face_recognition
import pickle
import time
from threading import Thread
import cv2
import numpy as np

with open('train.pkl','rb')as f:
    Names = pickle.load(f)
    Encodings = pickle.load(f)



#视频流拉取类
class vStream:
    def __init__(self,src,width,height):
        #frame 初始尺寸
        self.width = width
        self.height = height
        self.capture = cv2.VideoCapture(src)
        self.thread = Thread(target=(self.update),args=())
        self.thread.daemon = True
        self.thread.start()
    def update(self):
        while True:
            _,self.frame = self.capture.read()
            self.frame2 = cv2.resize(self.frame,(self.width,self.height))

    def getFrame(self):
        return self.frame2

ip1 = 'rtsp://admin:qwertyuiop123@192.168.31.103/Streaming/Channels/2'
ip2 = 'rtsp://admin:JGSYS123@192.168.31.123/Streaming/Channels/2'

disW = 640
disH = 360
cam1 = vStream(ip1,disW,disH)
cam2 = vStream(ip2,disW,disH)
#显示帧率
font = cv2.FONT_HERSHEY_SIMPLEX
startTime = time.time()
dtav = 1
scaleFactor=.25

while True:
    try:
        myframe1 = cam1.getFrame()
        myframe2 = cam2.getFrame()
        #将myframe1,myframe2 放在同一个画面
        frameCombine = np.hstack((myframe1,myframe2))
        #将图像转换缩小,加载模型识别
        frameRGB = cv2.cvtColor(frameCombine, cv2.COLOR_BGR2RGB)
        frameRGBSmall = cv2.resize(frameRGB, (0, 0), fx=scaleFactor, fy=scaleFactor)
        facePositions = face_recognition.face_locations(frameRGBSmall, model='cnn')
        allEncodings = face_recognition.face_encodings(frameRGBSmall, facePositions)

        for (top, right, bottom, left), face_encoding in zip(facePositions, allEncodings):
            name = 'Unkown Person'
            matches = face_recognition.compare_faces(Encodings, face_encoding)
            if True in matches:
                first_match_index = matches.index(True)
                name = Names[first_match_index]
                # 将检测框放大scalefactor倍,上面图片是缩小三倍检测的结果,检测框也是缩小三倍的
            top = int(top / scaleFactor)
            right = int(right / scaleFactor)
            bottom = int(bottom / scaleFactor)
            left = int(left / scaleFactor)
            cv2.rectangle(frameCombine, (left, top), (right, bottom), (0, 0, 255), 2)
            cv2.putText(frameCombine, name, (left, top - 6), font, .75, (0, 0, 255), 2)

        #显示帧率
        dt = time.time() - startTime
        startTime = time.time()
        # 低通滤波,避免fps误差太大
        dtav = .90 * dtav + .1 * dt
        fps = 1 / dtav
        fps = round(fps, 2)
        # print('fps is:',round(dtav,1))
        cv2.rectangle(frameCombine, (0, 0), (100, 40), (0, 0, 255), -1)
        cv2.putText(frameCombine, str(round(fps, 1)) + 'fps', (0, 25), font, .75, (0, 255, 255, 2))
        cv2.imshow('combine frame', frameCombine)

    except:
        print('frame not available')

    keyCode = cv2.waitKey(1) & 0xFF
    if keyCode == 27:  # ESC键退出
        cam1.capture.release()
        cam2.capture.release()
        cv2.destroyAllWindows()
        exit(1)
        break
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,我了解你的问题。Java OpenCV 可以通过调用摄像头实现动态识别人脸。以下是一个简单的示例代码,可以帮助你开始: 首先,你需要在你的项目中引入 OpenCV 库。可以通过以下步骤实现: 1.下载 OpenCV 库。 2.在你的 Java 项目中创建一个名为 "lib" 的文件夹。 3.将下载的 OpenCV 库复制到 "lib" 文件夹中。 4.在你的项目中添加 OpenCV 库的路径。在 Eclipse 中,可以通过右键单击项目,选择 "Properties",然后选择 "Java Build Path"。单击 "Add External JARs",然后选择 "lib" 文件夹中的相应 OpenCV 库文件。 接下来,你可以使用以下代码调用摄像头并实现人脸识别: ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import org.opencv.videoio.VideoCapture; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class FaceDetection extends JPanel implements Runnable { private static final long serialVersionUID = 1L; private ScheduledExecutorService timer; private VideoCapture capture; private CascadeClassifier faceCascade; private Mat grayscaleImage; private BufferedImage currentFrame; public FaceDetection() { super(); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); this.capture = new VideoCapture(0); this.faceCascade = new CascadeClassifier("haarcascade_frontalface_alt.xml"); this.grayscaleImage = new Mat(); this.currentFrame = new BufferedImage(640, 480, BufferedImage.TYPE_3BYTE_BGR); } @Override public void run() { Mat frame = new Mat(); if (this.capture.isOpened()) { try { this.capture.read(frame); if (!frame.empty()) { detectAndDisplay(frame); } } catch (Exception e) { System.err.println("Exception during the image elaboration: " + e); } } } private void detectAndDisplay(Mat frame) { MatOfRect faces = new MatOfRect(); Imgproc.cvtColor(frame, grayscaleImage, Imgproc.COLOR_BGR2GRAY); Imgproc.equalizeHist(grayscaleImage, grayscaleImage); faceCascade.detectMultiScale(grayscaleImage, faces); Rect[] facesArray = faces.toArray(); for (Rect rect : facesArray) { Imgproc.rectangle(frame, rect.tl(), rect.br(), new Scalar(0, 255, 0), 3); } currentFrame = matToBufferedImage(frame); } private BufferedImage matToBufferedImage(Mat original) { BufferedImage image = null; int width = original.width(), height = original.height(), channels = original.channels(); byte[] sourcePixels = new byte[width * height * channels]; original.get(0, 0, sourcePixels); if (original.channels() > 1) { image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return image; } public void start() { this.timer = Executors.newSingleThreadScheduledExecutor(); this.timer.scheduleAtFixedRate(this, 0, 33, TimeUnit.MILLISECONDS); } public void stop() { try { this.timer.shutdown(); this.timer.awaitTermination(33, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (this.capture.isOpened()) { this.capture.release(); } } } @Override public void paint(Graphics g) { super.paint(g); if (this.currentFrame != null) { g.drawImage(this.currentFrame, 0, 0, getWidth(), getHeight(), null); } } public static void main(String[] args) { JFrame frame = new JFrame("Face Detection"); FaceDetection faceDetection = new FaceDetection(); frame.setContentPane(faceDetection); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(640, 480); frame.setVisible(true); faceDetection.start(); } } ``` 在上面的代码中,我们创建了一个名为 "FaceDetection" 的类,该类扩展了 JPanel 和 Runnable 接口。我们创建了一个定时器来定期从摄像头读取图像,并在图像中进行人脸检测。我们使用 Haar 级联分类器来检测人脸,通过将图像转换为灰度图像,然后使用 equalizeHist() 函数来增强图像的对比度。最后,我们将检测到的人脸用矩形框标记出来,并将图像显示在应用程序窗口中。 希望这可以帮助你开始使用 Java OpenCV 进行动态人脸识别。 ### 回答2: Java与OpenCV可以配合使用来调用摄像头并实现动态人脸识别。 在Java中调用摄像头,我们可以使用OpenCV库中的VideoCapture类。首先,我们需要导入OpenCV库并加载相应的本地库文件。 ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.VideoCapture; import org.opencv.core.CvType; public class CameraCapture { public static void main(String[] args) { // 加载OpenCV库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 创建VideoCapture对象并打开摄像头 VideoCapture capture = new VideoCapture(0); // 参数0表示默认摄像头 // 检查摄像头是否成功打开 if (!capture.isOpened()) { System.out.println("摄像头未成功打开!"); return; } // 读取摄像头中的每一帧图像并进行处理 Mat frame = new Mat(); while (true) { // 读取一帧图像 capture.read(frame); // 进行人脸识别处理... // 显示图像窗口 HighGui.imshow("Camera Capture", frame); // 等待用户按下ESC键退出窗口 if (HighGui.waitKey(1) == 27) { break; } } // 释放资源 capture.release(); HighGui.destroyAllWindows(); } } ``` 上述代码中,我们使用VideoCapture类打开摄像头并循环读取每一帧图像。我们可以在循环中加入人脸识别的代码,例如使用OpenCV人脸识别功能来检测人脸并标记出来。 在循环中,我们使用HighGui.imshow()方法将每一帧图像显示在图像窗口中,再通过HighGui.waitKey()方法等待用户按下ESC键退出窗口。 最后,我们在程序结束时释放资源,包括关闭摄像头和销毁图像窗口。 通过以上代码,我们可以实现使用Java调用摄像头并动态识别人脸的功能。 ### 回答3: Java通过OpenCV库可以调用摄像头实时识别人脸。OpenCV是一个开源的计算机视觉库,可以在Java中使用它进行图像处理和分析。 首先,需要下载并安装OpenCV库,并将其添加到Java项目的构建路径中。 接下来,需要使用Java的图像处理API结合OpenCV来实现人脸识别。可以使用OpenCV的CascadeClassifier类加载一个已经训练好的人脸识别模型,例如使用Haar分类器训练的模型。 在调用摄像头之前,需要初始化摄像头设备。可以使用Java的javax.swing包中的JFrame和JPanel类创建一个界面窗口,并在窗口中显示摄像头捕获的图像。 使用Java的VideoCapture类从摄像头读取图像帧。然后,使用OpenCV的Mat类将图像数据转换为OpenCV的图像格式。 通过调用人脸识别模型的detectMultiScale方法,可以在图像中检测到人脸,并返回人脸位置的矩形。 最后,在图像上绘制矩形框来标记人脸的位置,并将处理后的图像显示在界面窗口中。 这样,Java就能够调用摄像头实时识别人脸了。可以将这个功能应用于人脸识别门禁系统、人脸表情识别等实际应用中,提高系统的智能化和便捷性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值