python调用摄像头

摘要:
1.当参数为0时,Python调用本地相机importnumpyasnpimportcv2cap=cv2.VideoCapture(0)#;URL连接调用网络摄像头;While(True):ret,frame=cap。read()#灰度灰=cv2.cvtColor(帧,cv2.COLOR_BGR2GRAY)cv2.imshow('帧

1.python实时调取本地摄像头

import numpy as np
import cv2
cap = cv2.VideoCapture(0)    #参数为0时调用本地摄像头;url连接调取网络摄像头;文件地址获取本地视频
while(True):
ret,frame=cap.read()

#灰度化
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)

#普通图片
cv2.imshow('frame',frame)

if cv2.waitKey(1)&0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()

2.opencv代码

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

#设置工作路径
import os
os.chdir('E:\0yfl\SH-spyder-workspace\')
os.path.abspath('.')


import numpy as np
import cv2

#1.1读取图片imread;展示图片imshow;导出图片imwrite
#只是灰度图片
img=cv2.imread('Myhero.jpg',cv2.IMREAD_GRAYSCALE)
#彩色图片
img=cv2.imread('Myhero.jpg',cv2.IMREAD_COLOR)
#彩色以及带有透明度
img=cv2.imread('Myhero.jpg',cv2.IMREAD_UNCHANGED)
print(img)
#设置窗口可自动调节大小
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
k=cv2.waitKey(0)
#如果输入esc
if k==27:
    #exit
    cv2.destroyAllWindows
#如果输入s
elif k==ord('s'):
    #save picture and exit
    cv2.imwrite('Myhero_out.png',img)
    cv2.destroyAllWindows()


#1.2视频读取
#打开内置摄像头
cap=cv2.VideoCapture(0)
#打开视频
cap=cv2.VideoCapture('why.mp4')
#或者视频每秒多少帧的数据
fps=cap.get(5)
i=0
while(True):
    #读取一帧
    ret,frame=cap.read()
    #转化为灰图
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    #设置导出文件名编号
    i = i + 1
    #每1s导出一张
    if i/fps==int(i/fps):
        #导出文件名为why+编号+.png
        #若想要导出灰图,则将下面frame改为gray即可
        cv2.imwrite("why"+str(int(i/fps))+".png",frame)
    #读完之后结束退出
    if cv2.waitKey(1)==ord('q'):
        break

cap.release()
cv2.destoryAllWindows()


#1.3图像像素修改
rangexmin=100
rangexmax=120
rangeymin=90
rangeymax=100
img=cv2.imread('Myhero.jpg',0)
img[rangexmin:rangexmax,rangeymin:rangeymax]=[[255]*(rangeymax-rangeymin)]*(rangexmax-rangexmin)
cv2.imwrite('Myhero_out2.png',img)

#拆分以及合并图像通道1
b,g,r=cv2.split(img)
img=cv2.merge(b,g,r)

#png转eps,不过非常模糊
from matplotlib import pyplot as plt
img=cv2.imread('wechat1.png',cv2.IMREAD_COLOR)
plt.imsave('wechat_out.eps',img)

#图像按比例混合
img1=cv2.imread('Myhero.jpg',cv2.IMREAD_COLOR)
img2=cv2.imread('Myhero_out.png',cv2.IMREAD_COLOR)
dst=cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.imwrite("Myhero_combi.jpg",dst)


#1.4按位运算
#加载图像
img1=cv2.imread("Myhero.jpg")
img2=cv2.imread("why1.png")
#后面那张图更大
rows,cols,channels=img1.shape
ROI=img2[0:rows,0:cols]
#做一个ROI为图像的大小
img2gray=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
#小于175的改为0,大于175的赋值为255
ret,mask=cv2.threshold(img2gray,175,255,cv2.THRESH_BINARY)
cv2.imwrite("Myhero_mask.jpg",mask)
#255-mask=mask_inv
mask_inv=cv2.bitwise_not(mask)
cv2.imwrite("Myhero_mask_inv.jpg",mask_inv)
#在mask白色区域显示成ROI,背景图片
img2_bg=cv2.bitwise_and(ROI,ROI,mask=mask)
cv2.imwrite("Myhero_pic2_backgroud.jpg",img2_bg)
#除了mask以外的区域都显示成img1,前景图片
img1_fg=cv2.bitwise_and(img1,img1,mask=mask_inv)
cv2.imwrite("Myhero_pic2_frontgroud.jpg",img1_fg)
#前景图片加上背景图片
dst = cv2.add(img2_bg,img1_fg)
img2[0:rows, 0:cols ] = dst
cv2.imwrite("Myhero_pic2_addgroud.jpg",dst)
#finished

#构建淹膜方法2
#截取帧
ret,frame=cap.read()
#转换到HSV
hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
#设定蓝色的阈值
lower_blue=np.array([110,50,50])
upper_blue=np.array([130,255,255])
#根据阈值构建掩模
mask=cv2.inRange(hsv,lower_blue,upper_blue)
#对原图像和掩模进行位运算
res=cv2.bitwise_and(frame,frame,mask=mask)


#图片放缩,用的插值方法,所以不会损害清晰度
res=cv2.resize(img1,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
cv2.imwrite("Myhero_bigger.jpg",res)
#第二种插值方法
height,width=img.shape[:2]
res=cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)

#edge现实图片中不好用,人工画的图片还可以
img = cv2.imread('why3.png',0)
edges = cv2.Canny(img,50,100)
cv2.imwrite("why3_edge.png",edges)

#识别轮廓,并保存轮廓点contours
img=cv2.imread('why129.png')
imgray=cv2.imread('why129.png',cv2.IMREAD_GRAYSCALE)
ret,thresh = cv2.threshold(imgray,127,255,0)
cv2.imwrite("2.jpg",thresh)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(img, contours, -1, (0,255,0), 3)
cv2.imwrite("3.jpg",img)


#轮廓
img = cv2.imread('why3.png',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0] 
#近似轮廓
epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)

img = cv2.drawContours(img, approx, -1, (0,255,0), 3)
cv2.imwrite("4.jpg",img)

from matplotlib import pyplot as plt
#图像识别/匹配
img_rgb = cv2.imread('why174.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
img2=img_gray.copy()
template = cv2.imread('0temp.png',0)
w, h = template.shape[::-1]
#共有六种识别方法
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

for meth in methods:
    img = img2.copy()
    #eval返回某个式子的计算结果
    method = eval(meth)
    #下面使用匹配方法
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    #画矩形把他框出来
    cv2.rectangle(img,top_left, bottom_right, 255, 2)
    
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    
    plt.show()
    
#这个匹配结果太差
#选取3,5,6的匹配方式会稍微好点:cv2.TM_CCORR;cv2.TM_SQDIFF,cv2.TM_SQDIFF_NORMED

#视频人脸识别
#https://blog.csdn.net/wsywb111/article/details/79152425
import cv2
from PIL import Image
cap=cv2.VideoCapture("why.mp4")
#告诉Opencv使用人脸识别分类器
classfier=cv2.CascadeClassifier("E:\0yfl\opencv-master\data\haarcascades\haarcascade_frontalface_alt2.xml")
count=0
while cap.isOpened():
    ret,frame=cap.read()
    if not ret:
        break
    grey=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faceRect=classfier.detectMultiScale(grey,scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
    if len(faceRect)>0:
        count=count+1
print(count)


#137这种程度可以识别,111没有成功识别,大概是侧脸的缘故
#截出人脸
image_name="why111.png"
frame=cv2.imread(image_name,0)
if not (frame is None):
    #导入测试集
    classfier=cv2.CascadeClassifier("E:\0yfl\opencv-master\data\haarcascades\haarcascade_frontalface_alt2.xml")
    #使用测试集导出人脸的位置,存在faceRect中,可以检测多张人脸
    faceRect=classfier.detectMultiScale(frame,scaleFactor=3.0, minNeighbors=3, minSize=(32, 32))
    count=0
    for (x1,y1,w,h) in faceRect:
        count=count+1
        #截取上述图片的人脸部分并保存每一张识别出的人脸
        Image.open(image_name).crop((x1,y1,x1+w,y1+h)).save(image_name.split(".")[0]+"_face_"+str(count)+".png")
    if count==0:
        print ("No face detected!")
else:
    print ("Picture "+ image_name +" is not exist in "+os.path.abspath("."))
#人脸上画出矩形
from PIL import Image,ImageDraw
image_name="why111.png"
frame=cv2.imread(image_name,0)
if not (frame is None):
    classfier=cv2.CascadeClassifier("E:\0yfl\opencv-master\data\haarcascades\haarcascade_frontalface_alt2.xml")
    faceRect=classfier.detectMultiScale(frame,scaleFactor=3.0, minNeighbors=3, minSize=(32, 32))
    #画框框
    img = Image.open(image_name)
    draw_instance = ImageDraw.Draw(img)
    count=0
    for (x1,y1,w,h) in faceRect:
        draw_instance.rectangle((x1,y1,x1+w,y1+h), outline=(255, 0,0))
        img.save('drawfaces_'+image_name)
        count=count+1
    if count==0:
        print ("No face detected!")
else:
    print ("Picture "+ image_name +" is not exist in "+os.path.abspath("."))


#detectFaces()返回图像中所有人脸的矩形坐标(矩形左上、右下顶点)
#使用haar特征的级联分类器haarcascade_frontalface_default.xml,在haarcascades目录下还有其他的训练好的xml文件可供选择。
#注:haarcascades目录下训练好的分类器必须以灰度图作为输入。


from PIL import Image,ImageDraw
image_name="why63.png"
frame=cv2.imread(image_name,0)
if not (frame is None):
    classfier=cv2.CascadeClassifier("E:\0yfl\opencv-master\data\haarcascades\haarcascade_fullbody.xml")
    faceRect=classfier.detectMultiScale(frame,scaleFactor=3.0, minNeighbors=3, minSize=(32, 32))
    #画框框
    img = Image.open(image_name)
    draw_instance = ImageDraw.Draw(img)
    count=0
    for (x1,y1,w,h) in faceRect:
        draw_instance.rectangle((x1,y1,x1+w,y1+h), outline=(255, 0,0))
        img.save('drawfaces_'+image_name)
        count=count+1
    if count==0:
        print ("No face detected!")
else:
    print ("Picture "+ image_name +" is not exist in "+os.path.abspath("."))

免责声明:文章转载自《python调用摄像头》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇1-匿名对象送你一份Redis书单,以后使用缓存的问题不用再问我啦!下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python 基础 5.1 python 构造器

一. 类的构造器 __init__ 构造函数,在生成对象时调用。由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。通过定义一个特殊的__init__方法,在创建实例的时候,就把 name score 等属性上去。默认的属性可以写在__init__ 下面。 #/usr/bin/python #coding=ut...

Flask web应用

Flask web应用一、介绍 最近开发要用一个测试环境,是这样的Nginx+uwsgi+flask 的一个结构。下面是一些记录,在Centos 系统上使用Flask 架构部署一个简单的Python应用。然后使用Nginx作为前端反向代理,设置uWSGI应用网关处理web应用程序。 二、条件 1) 环境要求 Server OS:最小化安装 Centos...

指令脚本redis线上环境监控脚本(python脚本)

在改章节中,我们要主介绍指令脚本的内容,自我觉感有个不错的议建和大家分享下 近来一个月没啥新更,边身生发太多事,结业几年来霉运太多,虽然不信命,但我信有些性命的确好,有些性命的确差,其它不说也罢。(大家定一要意注身材啊,康健比任何西东都主要) 本文要监控的这个脚本,是在一个月前阁下,对于线上redis中项指令操纵须要行进统计析分时写的一个工具: 一.需求...

Python 离线环境

一、应用场景 比如:对于数据安全要求比较严格的机房,服务器是不允许上网的。那么我现在开发了一套python程序,需要一些模块,怎么运行? 二、离线包制作 有2个解决方案: 1. 使用requirement.txt离线安装打包好的包whl 请参考链接:https://blog.csdn.net/wangyaninglm/article/details/541...

C#程序执行Python脚本

方法介绍:      通过调用“Python.exe”程序,执行脚本文件。所以,本方式要求电脑上已经安装了Python,拥有程序Python.exe程序。 现在,有如下py脚本:Add.py import sys def Add(a,b): return a+b if __name__=='__main__': X = int(sys....

python 中__setattr__, __getattr__,__getattribute__, __call__使用方法

object._getattr_(self, name) 拦截点号运算。当对未定义的属性名称和实例进行点号运算时,就会用属性名作为字符串调用这个方法。如果继承树可以找到该属性,则不调用此方法 实例instance通过instance.name访问属性name,只有当属性name没有在实例的__dict__或它构造类的__dict__或基类的__dict__...