简体   繁体   English

将 IP 网络摄像头与 OpenCV 运动跟踪结合使用

[英]Using IP Webcam with OpenCV motion tracking

I'm trying to make a tracking laser webcam toy for my cat, but I am currently struggling with the IP webcam from an Android phone as it won't display anything and i get an error "initStream Failed to reset streams" I have attached the code below!我正在尝试为我的猫制作跟踪激光网络摄像头玩具,但我目前正在努力使用来自 Android 手机的 IP 网络摄像头,因为它不会显示任何内容并且我收到错误“initStream 无法重置流”我已附加下面的代码! I'm still new to Python and would love to learn more!我还是 Python 新手,很想学习更多! =) =)

import cv2
import numpy as np
#Cam
url = "http://192.168.x.x:8080/shot.jpg"
img_resp = requests.get(url)
img_arr = np.array(bytearray(img_resp.content),dtype=np.uint8)
img = cv2.imdecode(img_arr,-1)
cap =cv2.VideoCapture(0)
ret, frame = cap.read()
#Movement tracker
while cap.isOpened():
    ret = cap.set(3,320)
    ret = cap.set(4,240)
    diff = cv2.absdiff(frame, frame)
    gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
    dilated = cv2.dilate(thresh, None, iterations=3)
    contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        (x, y, w, h) = cv2.boundingRect(contour)
        if cv2.contourArea(contour) < 900:
            continue
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 20, 30), 3)
        cv2.putText(frame, "Status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 3)
        cv2.circle(frame, (x, y), 3, (200, 50, 180), 2)

        image = cv2.resize(frame, (1280, 720))
        cv2.imshow("feed", frame)
        ret, frame2 = img.read()

        if cv2.waitKey(1) == 27:
            break
        cv2.destroyAllWindows()
        img.release()

I use IP Webcam application on my android phone.我在我的 android 手机上使用IP 网络摄像头应用程序。 Just need to define url variable as below:只需要定义url变量如下:

url = 'http://your IP address:port number/video' # e.g. url = 'http://192.168.43.1:8080/video'

You will see IP address and port number on the mobile phone screen when the app is running.应用程序运行时,您将在手机屏幕上看到IP 地址端口号 And after that you need to pass url as an argument to cv2.VideoCapture() method like this cap = cv2.VideoCapture(url) instead of cap = cv2.VideoCapture(0) .之后,您需要将url作为参数传递给cv2.VideoCapture()方法,例如cap = cv2.VideoCapture(url)而不是cap = cv2.VideoCapture(0) It works fine for me.这对我来说可以。 No need to say that PC and mobile phone are connected via Wi-Fi network (or mobile hotspot).不用说PC和手机是通过Wi-Fi网络(或移动热点)连接的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM