The following is to share a Python OpenCV set the camera resolution and the various parameters of the method, has a good reference value, I hope to be helpful to everyone. Come and see it together.
1, in order to get the video, you should create a Videocapture object. His parameters can be the index number of the device, or a video file. The device index number is the camera that you specify to use. Typical laptops have built-in cameras. So the parameter is 0. You can choose a different camera by setting it to 1 or something else. After that, you can capture the video in one frame at a frame. But finally, don't forget to stop capturing the video. Use the ls/dev/video* command to view the camera device
2,cap.read () returns a Boolean value (True/false). if the frame reads correctly, it is True. So finally you can check his return value to see if the video file has reached the end. Sometimes the cap may not successfully initialize the camera device. In this case, the above code will error. You can use cap.isopened () to check whether the initialization was successful. If the return value is true, then there is no problem. Otherwise you will use the function Cap.open (). You can use the function Cap.get (propid) to get some parameter information for the video. Here propid can be any integer between 0 and 18. Each number represents a property of the video, and some of the values in the table can be modified using Cap.set (Propid,value), and value is
The new value you want to set. For example, I can use Cap.get (3) and Cap.get (4) to see the width and height of each frame. The value obtained by default is 640x480. But I can use Ret=cap.set (3,320) and Ret=cap.set (4,240) to change the width to the high.
Cv_cap_prop_pos_msec current position of the video file in milliseconds. cv_cap_prop_pos_frames 0-based index of the Fram E to be decoded/captured next. cv_cap_prop_pos_avi_ratio Relative position of the video file:0-start of the film, 1- End of the Film. cv_cap_prop_frame_width WIDTH of the frames in the video Stream. cv_cap_prop_frame_height HEIGHT of the Frames in the video stream. cv_cap_prop_fps Frame rate. CV_CAP_PROP_FOURCC 4-character Code of Codec. Cv_cap_prop_fram E_count number of frames in the video file. Cv_cap_prop_format FORMAT of the Matt objects returned by retrieve (). Cv_cap _prop_mode backend-specific value indicating the current capture Mode. cv_cap_prop_brightness brightness of the image (on ly for cameras). cv_cap_prop_contrast Contrast of the "image" (only for cameras). cv_cap_prop_saturation Saturation of the Image (only for cameras). Cv_cap_prop_hue HUE of the image (only for cameras). Cv_cap_prop_gain GAIN of the image (only For cameras). cv_cap_pRop_exposure EXPOSURE (only for cameras). Cv_cap_prop_convert_rgb Boolean Flags whether images should is converted to RGB . Indicating cv_cap_prop_white_balance currently unsupported cv_cap_prop_rectification RECTIFICATION flag for Stereo Cameras (Note:only supported by DC1394 v 2.x backend cur-rently
#!/usr/bin/env python#-*-coding:utf-8-*-import cv2import numpyfrom hlf_module import hlf_definefrom std_msgs.msg impor T Stringimport Matplotlib.pyplot as Plotimport xml.dom.minidomimport pylabimport rospyimport timecap = Cv2. Videocapture (0) cap.set (3,640) #设置分辨率cap. Set (4,480) FPS =cap.get (cv2. Cap_prop_fps) #获取视频帧数face_casade = Cv2. Cascadeclassifier ('/opt/ros/kinetic/share/opencv-3.2.0-dev/haarcascades/haarcascade_frontalface_default.xml ') Node_name= ' neck ' #print cap.isopened () class Detect_face ():d ef __init__ (self): "' Defines node Node_name (global variable, not specific name) ' Self.err_pub=hlf_define.err_publisher () #错误消息发布者rospy. Init_node (node_name,anonymous=true) self.neck_puber=rospy. Publisher (Hlf_define. topic_action_neck,string,queue_size=10) Time.sleep (0.5) def head_motor_value (self): #解析xml文件 get the range value of the rudder dom = Xml.dom.minidom.parse ('/home/sb/catkin_ws/src/hlf_robot/scripts/hlf_action/head_value.xml ') #得到文档元素对象root = Dom.documentelementitemlist = root.getelementsbytagname (' login ') item = Itemlist[0]max_value=item.getattribUte ("Max") Min_value=item.getattribute ("Min") return max_value,min_valuedef Detect_face (self): # get a frame#frame= Cv2.imread ('/home/sb/desktop/timg.jpeg ') ret, frame = Cap.read () Gray = Cv2.cvtcolor (frame,cv2. Color_bgr2gray) #转成灰度图 #frame=cv2.cvtcolor (Frame,cv2. Color_bgr2gray) # Show a framecv2.imshow ("capture", gray) faces = Face_casade.detectmultiscale (gray,1.2,5) #检测人脸 #print Len (Faces) If Len (faces) >0: #判断是否检测到人脸result = () Max_face = 0value_x=0for (x,y,w,h) in Faces:if (W*h > Max_face): #检测最 Adult face max_face = W*hresult = (x,y,w,h) # max_face.append (width*height) x=result[0]w=result[2]z=value_x=value_x+x+w/ 2return zelse:return 1if __name__== ' __main__ ': Face=detect_face () motor_max,motor_min= face.head_motor_value () x=[]i= 1while true:try:z=face.detect_face () if z! = 1:x.append (z) If Len (x) > (fps-1): true_x = Int (sum (x)/30) if (true_x>319 ): Motor_value=int (1500+ (int (motor_max) -1500) * (true_x-319)/320) #转换成舵机值 head to the left Face.neck_puber.publish ('%s '%motor_ Value) elif (true_x<319): Motor_value=int (1500-(1500-int (MOtor_min)) * (319-true_x)/320) face.neck_puber.publish ('%s '%motor_value) x=[]else:if i==fps:face.neck_puber.publish (' I=1else:i ') +=1print (U ' not detected face ') if Cv2.waitkey (1) & 0xFF = = Ord (' q '): Breakexcept exception,e:print Ecap.release () cv2.destroyallwindows ()