opencv(by python)图像识别的教程代码

#################1

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray) #imgshow
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
#第一课,打开电脑摄像头,调节灰色并显示,q键停止,关闭退出

    #名称为gray


#################2

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#创建
def make_1080p():
    cap.set(3,1920)
    cap.set(4,1080)
def make_720p():
    cap.set(3,1280)
    cap.set(4,720)
def make_480p():
    cap.set(3,640)
    cap.set(4,480)
def change_res(width,height):
    cap.set(3,width)
    cap.set(4,height) 
#以上是定义的更改窗口大小的函数,change_res为自定义 
def rescale_frame(frame,percent=75):
    scale_percent = 75
    width = int(frame.shape[1] * scale_percent / 100)
    height = int(frame.shape[0] * scale_percent / 100)
    dim = (width,height)
    return cv2.resize(frame,dim,interpolation = cv2.INTER_AREA)
#函数调整窗口的百分比大小
make_1080p()
while True:
    ret,frame = cap.read()
    frame = rescale_frame(frame,percent = 30)
    cv2.imshow('frame',frame)
    frame2= rescale_frame(frame,percent = 140)
    cv2.imshow('frame2',frame2)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()

#释放空间,关闭窗口



#################3

import os
import numpy as np
import cv2
filename = 'video.avi' #avi.mp4
frames_per_seconds = 24.0 #帧数
my_res = '480p' #1080p
#set resolution for the video capture
def change_res(cap,width,height):
    cap.set(3,width)
    cap.set(4,height)
#stadard video dimensions sizes
STD_DIMENSIONS = {
    "480p":(640,480),
    "720p":(1280,720),
    "1080p":(1920,1080),
    "4k":(3840,2160),
}
def get_dims(cap,res='1080p'):
    width,height = STD_DIMENSIONS['480p']#超出标准范围
    if res in STD_DIMENSIONS:
        width,height = STD_DIMENSIONS[res]
    change_res(cap,width,height)
    return width,height
VIDEO_TYPE = {
    'avi':cv2.VideoWriter_fourcc(*'XVID'),
    #'mp4':cv2.VideoWriter_fourcc(*'H264'),
    'mp4':cv2.VideoWriter_fourcc(*'XVID'),
}
#用户设置需要保存的录像格式
def get_video_type(filename):
    fiename,ext = os.path.splitext(filename)#分离文件名和拓展名
    if ext in VIDEO_TYPE:
        return VIDEO_TYPE[ext]
    return VIDEO_TYPE['avi']
cap = cv2.VideoCapture(0)
dims = get_dims(cap,res=my_res)
video_type_cv2 = get_video_type(filename)
out = cv2.VideoWriter(filename,video_type_cv2,frames_per_seconds,dims)
while True:
    ret,frame = cap.read()
    #frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    out.write(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
# when everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()
#释放空间,关闭窗口
#EOL while scanning string literal///////////////////////
#是因为引号或者其他符号没有对齐
#Traceback (most recent call last):
# File "D:/zuoye/python/test_3.py", line 33, in <module>
#   'mp4':cv2.VideoWriter_fourcc('*XVID'),
#TypeError: Required argument 'c2' (pos 2) not found///////////////////
#原因是要写成*'XVID',引号在*后面

#灰色录制视频无法播放


#################4

没有写完,添加cmd

cd d:切换到D

打开里面的文件夹要用cd python没有冒号

dir用于查看所有文件

python test_5.py执行


import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while True:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5)
    for (x,y,w,h) in faces:
        print(x,y,w,h)
        roi_gray = gray[y:y+h,x:x+w] #(ycord_start,ycord_end)

        roi_color = frame[y:y+h,x:x+w]


        img_item = "my_imagine.png"
        cv2.imwrite(img_item,roi_gray)
        color = (255,0,0) #BGR 0-255
        stroke = 2
        end_cord_x = x + w
        end_cord_y = y + h
        cv2.rectangle(frame,(x,y),(end_cord_x,end_cord_y),color,stroke)
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
# when everything done, release the capture
cap.release()
cv2.destroyAllWindows()


阅读更多
文章标签: opencv python
个人分类: Opencv python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

opencv(by python)图像识别的教程代码

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭