opencv获取网络相机的图像-不用sdk


 

 

海康相机

优点:不用sdk直接网络获取

缺点:速度有1-2秒的延迟

使用型号

1280*680分辨路

#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	//1.从摄像头读入视频
	//VideoCapture cap(0);

	VideoCapture cap("rtsp://admin:fhy145145@192.168.1.62/Streaming/Channels/2"); //1 主码流 1 1280*680   2 次码流620*480
	if (!cap.isOpened())
	{
		return 0;
	}

	Mat cam;
	cap >> cam;//获取当前帧图像
	cout << "Im.rows  = " << cam.rows << endl;
	cout << "Im.cols  = " << cam.cols << endl;

	//cout << "视频宽度=" << width << endl;
	//cout << "视频高度=" << height << endl;

	//2.循环显示每一帧
	while (1)
	{
		
		cap >> cam;//获取当前帧图像
		if (!cam.empty())//如果某帧为空则退出循环
		{
	    namedWindow("相机", 0);
		imshow("相机", cam);//显示当前帧图像
     
	   waitKey(30);//延时30秒
		}
	}
	return 0;
}

  

注: admin和12345分别是ip camera的用户名和密码,在浏览器上第一次登录摄像头的时候会进行设置;
192.168.1.64是摄像头的默认IP,在浏览器中输入即可进入登录页面(如下);
947235-20170717141904956-1178222844.png
Channels/1和Channels/2分别对应主码流和子码流地址,两者的分辨率不同。IP摄像头无法通过opencv调节分辨率,只能在海康的监控界面的配置进行设置(如下)。
947235-20170717142202097-697874552.png

3. 摄像头校准(calibration)(optional)

这个配置有很多,而且官方提供的校准文档很详细(戳这里),就不具体细讲了。需要python版本的代码的话可以直接fork这里;如果是c++版本的话可以参考这里

 

python

相机

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import cv2
import os
font = cv2.FONT_HERSHEY_SIMPLEX
#url='rtsp://admin:fhy145145@192.168.1.62/Streaming/Channels/1' #海康 
url = 'http://192.168.1.89/webcapture.jpg?command=snap&channel=1' #雄迈
#url='rtsp://192.168.1.89:554/user=admin&password=&channel=1&stream=0.sdp?'
cam = cv2.VideoCapture(url)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# For each person, enter one numeric face id
face_id = input('\n 请输入用户数字编号(只能是数字):   ')
print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face countdsa
count = 0
while(True):
    ret, img = cam.read()
    cv2.putText(img, str(count), (5,80), font, 1, (255,255,255), 2)
   # cv2.putText(img, str("change face!"), (5,40), font, 1, (255,255,255), 2)
 
   # img = cv2.flip(img, -1) # flip video image vertically
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)   #
        cv2.putText(img, str("Perss s to save face!"), (5,40), font, 1, (255,255,255), 2)
       # cv2.imshow('image', img)
        k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
        if k == ord('s'):
            count += 1
            cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])
            break
        elif k == 27:
            break
    cv2.imshow('image', img)    
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
    elif count >= 30: # Take 30 face sample and stop video
         break
    
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM