侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python开启摄像头以及深度学习实现目标检测方法

Python  /  管理员 发布于 7年前   214

最近想做实时目标检测,需要用到python开启摄像头,我手上只有两个uvc免驱的摄像头,性能一般。利用python开启摄像头费了一番功夫,主要原因是我的摄像头都不能用cv2的VideCapture打开,这让我联想到原来opencv也打不开Android手机上的摄像头(后来采用QML的Camera模块实现的)。看来opencv对于摄像头的兼容性仍然不是很完善。

我尝了几种办法:v4l2,v4l2_capture以及simpleCV,都打不开。最后采用pygame实现了摄像头的采集功能,这里直接给大家分享具体实现代码(python3.6,cv2,opencv3.3,ubuntu16.04)。中间注释的部分是我上述方法打开摄像头的尝试,说不定有适合自己的。

import pygame.cameraimport timeimport pygameimport cv2import numpy as np def surface_to_string(surface): """convert pygame surface into string""" return pygame.image.tostring(surface, 'RGB') def pygame_to_cvimage(surface): """conver pygame surface into cvimage"""  #cv_image = np.zeros(surface.get_size, np.uint8, 3) image_string = surface_to_string(surface) image_np = np.fromstring(image_string, np.uint8).reshape(480, 640, 3) frame = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) return image_np, frame  pygame.camera.init()pygame.camera.list_cameras()cam = pygame.camera.Camera("/dev/video0", [640, 480]) cam.start()time.sleep(0.1)screen = pygame.display.set_mode([640, 480]) while True: image = cam.get_image()  cv_image, frame = pygame_to_cvimage(image)  screen.fill([0, 0, 0]) screen.blit(image, (0, 0)) pygame.display.update() cv2.imshow('frame', frame) key = cv2.waitKey(1) if key & 0xFF == ord('q'):  break   #pygame.image.save(image, "pygame1.jpg") cam.stop()   

上述代码需要注意一个地方,就是pygame图片和opencv图片的转化(pygame_to_cvimage)有些地方采用cv.CreateImageHeader和SetData来实现,注意这两个函数在opencv3+后就消失了。因此采用numpy进行实现。

至于目标检测,由于现在网上有很多实现的方法,MobileNet等等。这里我不讲解具体原理,因为我的研究方向不是这个,这里直接把代码贴出来,亲测成功了。

from imutils.video import FPSimport argparseimport imutils  import v4l2import fcntl import v4l2captureimport selectimport image import pygame.cameraimport pygameimport cv2import numpy as npimport time def surface_to_string(surface): """convert pygame surface into string""" return pygame.image.tostring(surface, 'RGB') def pygame_to_cvimage(surface): """conver pygame surface into cvimage"""  #cv_image = np.zeros(surface.get_size, np.uint8, 3) image_string = surface_to_string(surface) image_np = np.fromstring(image_string, np.uint8).reshape(480, 640, 3) frame = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) return frame  ap = argparse.ArgumentParser()ap.add_argument("-p", "--prototxt", required=True, help="path to caffe deploy prototxt file")ap.add_argument("-m", "--model", required=True, help="path to caffe pretrained model")ap.add_argument("-c", "--confidence", type=float, default=0.2, help="minimum probability to filter weak detection")args = vars(ap.parse_args()) CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow",   "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) print("[INFO] loading model...")net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])  print("[INFO] starting video stream ...") ###### opencv #########vs = VideoStream(src=1).start()##camera = cv2.VideoCapture(0)#if not camera.isOpened():# print("camera is not open")#time.sleep(2.0)  ###### v4l2 ######## #vd = open('/dev/video0', 'r')#cp = v4l2.v4l2_capability()#fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp) #cp.driver  ##### v4l2_capture#video = v4l2capture.Video_device("/dev/video0")#size_x, size_y = video.set_format(640, 480, fourcc= 'MJPEG')#video.create_buffers(30) #video.queue_all_buffers() #video.start() ##### pygame ####pygame.camera.init()pygame.camera.list_cameras()cam = pygame.camera.Camera("/dev/video0", [640, 480]) cam.start()time.sleep(1) fps = FPS().start()  while True: #try: # frame = vs.read() #except: # print("camera is not opened")  #frame = imutils.resize(frame, width=400) #(h, w) = frame.shape[:2]   #grabbed, frame = camera.read() #if not grabbed: # break #select.select((video,), (), ()) #frame = video.read_and_queue()  #npfs = np.frombuffer(frame, dtype=np.uint8) #print(len(npfs)) #frame = cv2.imdecode(npfs, cv2.IMREAD_COLOR)  image = cam.get_image() frame = pygame_to_cvimage(image)  frame = imutils.resize(frame, width=640) blob = cv2.dnn.blobFromImage(frame, 0.00783, (640, 480), 127.5)  net.setInput(blob) detections = net.forward()  for i in np.arange(0, detections.shape[2]):   confidence = detections[0, 0, i, 2]   if confidence > args["confidence"]:    idx = int(detections[0, 0, i, 1])   box = detections[0, 0, i, 3:7]*np.array([640, 480, 640, 480])   (startX, startY, endX, endY) = box.astype("int")    label = "{}:{:.2f}%".format(CLASSES[idx], confidence*100)   cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)   y = startY - 15 if startY - 15 > 15 else startY + 15    cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)  cv2.imshow("Frame", frame) key = cv2.waitKey(1)& 0xFF  if key ==ord("q"):  break  fps.stop()print("[INFO] elapsed time :{:.2f}".format(fps.elapsed()))print("[INFO] approx. FPS :{:.2f}".format(fps.fps()))   cv2.destroyAllWindows() #vs.stop() 

上面的实现需要用到两个文件,是caffe实现好的模型,我直接上传(文件名为MobileNetSSD_deploy.caffemodel和MobileNetSSD_deploy.prototxt,上google能够下载到)。

以上这篇python开启摄像头以及深度学习实现目标检测方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    python调用摄像头显示图像的实例
    下一条:
    Python函数参数操作详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客