Python Dome(二)- 三种摄像头开启方式总结

前言

  为了方便图像处理,我们在python中就会去调用摄像头,通过三种方式去调用摄像头进行实时的图像处理或者拍照后的图像处理。

调用电脑摄像头

# -*- coding: utf-8 -*-
# !/usr/bin/env python3.9

import cv2

cap = cv2.VideoCapture(0)

index = 1
while (cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow("USB", frame)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('s'):  # 按下s(save)键,进入保存图片操作
        cv2.imwrite("C:/Users/inwinic/Desktop/img" + str(index) + ".jpg", frame)
        print("save fruit" + str(index) + ".jpg successfuly!")
        index += 1
    elif k == ord('q'):  # 按下q(quit)键,程序退出
        break
cap.release()
cv2.destroyAllWindows()

调用USB摄像头

# -*- coding: utf-8 -*-
# !/usr/bin/env python3.9

import cv2

cap = cv2.VideoCapture(1)

index = 1
while (cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow("USB", frame)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('s'):  # 按下s(save)键,进入保存图片操作
        cv2.imwrite("C:/Users/inwinic/Desktop/img" + str(index) + ".jpg", frame)
        print("save fruit" + str(index) + ".jpg successfuly!")
        index += 1
    elif k == ord('q'):  # 按下q(quit)键,程序退出
        break
cap.release()
cv2.destroyAllWindows()

这两种的差别是cap = cv2.VideoCapture(1),这里写入0的话就是调用电脑摄像头,写入1、2、3等,就是调用其他USB摄像头。

IP摄像头调用

# coding=utf-8
import cv2
import time
import os

if not os.path.exists('img'):
    os.mkdir('img')
filenames = os.listdir(r'img')

if __name__ == '__main__':

    # 开启ip摄像头
    cv2.namedWindow("camera", 1)
    # 这个地址就是下面记下来的局域网IP
    video = "http://admin:admin@xxx.xxx.xxx/"  # 此处@后的ipv4 地址需要修改为自己的地址

    capture = cv2.VideoCapture(video)

    num = len(filenames)
    index = 0
    imgname = -1
    while True:
        success, img = capture.read()

        # 不进行旋转
        cv2.imshow("camera", img)

        # 获取长宽
        # (h, w) = img.shape[:2]
        # center = (w // 2, h // 2)
        # 进行旋转
        # M = cv2.getRotationMatrix2D(center, -90, 1.0)
        # rotated = cv2.warpAffine(img, M, (w, h))
        # 若不关参数,参数也会被旋转,影响效果
        # cv2.imshow("camera", rotated)

        # 按键处理,注意,焦点应当在摄像头窗口,不是在终端命令行窗口
        key = cv2.waitKey(10)

        if key == 27:
            # 按esc键退出
            print("esc break...")
            break

        if key == ord(' '):
            # 按空格 保存图像 图片的路径
            while True:
                index = index + 1
                success, img = capture.read()
                cv2.imshow("camera", img)
                cv2.waitKey(10)
                if index == 15:
                    num = num + 1
                    imgname = imgname + 1
                    if imgname == -1:  # 此处改为-1为无限截取图片
                        break
                    filename = "img\\frames_%s.jpg" % (num)
                    cv2.imwrite(filename, img)
                    index = 0

    capture.release()
    cv2.destroyWindow("camera")

调用手机摄像头

也是调用IP摄像头

1、android手机上安装一款APP:IP摄像头,app的图片如上图

在这里插入图片描述

import cv2
 
cv2.namedWindow("camera", 1)
# 开启ip摄像头
video = "http://admin:admin@10.0.0.32:8081/"  # 此处@后的ipv4 地址需要改为app提供的地址
cap = cv2.VideoCapture(video)
#  开摄像头
while True:
    # Start Camera, while true, camera will runq
 
    ret, image_np = cap.read()
 
    # Set height and width of webcamq
    height = 600
    width = 1000
 
    # Set camera resolution and create a break function by pressing 'q'
    cv2.imshow('object detection',image_np)# cv2.resize(image_np, (width, height)))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
    print(cap.get(cv2.cv2.CAP_PROP_FRAME_COUNT))
# Clean up
cap.release()
cv2.destroyAllWindows()
 
#保存文件
 
fps = 30
 
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
 
# 调用VideoWrite()函数
videoWrite = cv2.VideoWriter('MySaveVideo.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
 
# 先获取一帧,用来判断是否成功调用摄像头
success, frame = cap.read()
 
# 通过设置帧数来设置时间,减一是因为上面已经获取过一帧了
numFrameRemainling = fps * 10 - 1
 
# 通过循环保存帧
while success and numFrameRemainling > 0:
    videoWrite.write(frame)
    success, frame = cap.read()
    numFrameRemainling -= 1
 
# 释放摄像头
cap.release()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

特辣番茄炒鸡蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值