欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python開啟攝像頭以及深度學(xué)習(xí)實(shí)現(xiàn)目標(biāo)檢測方法

 更新時間:2018年08月03日 09:15:05   作者:紅色未來  
今天小編就為大家分享一篇python開啟攝像頭以及深度學(xué)習(xí)實(shí)現(xiàn)目標(biāo)檢測方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近想做實(shí)時目標(biāo)檢測,需要用到python開啟攝像頭,我手上只有兩個uvc免驅(qū)的攝像頭,性能一般。利用python開啟攝像頭費(fèi)了一番功夫,主要原因是我的攝像頭都不能用cv2的VideCapture打開,這讓我聯(lián)想到原來opencv也打不開Android手機(jī)上的攝像頭(后來采用QML的Camera模塊實(shí)現(xiàn)的)。看來opencv對于攝像頭的兼容性仍然不是很完善。

我嘗了幾種辦法:v4l2,v4l2_capture以及simpleCV,都打不開。最后采用pygame實(shí)現(xiàn)了攝像頭的采集功能,這里直接給大家分享具體實(shí)現(xiàn)代碼(python3.6,cv2,opencv3.3,ubuntu16.04)。中間注釋的部分是我上述方法打開攝像頭的嘗試,說不定有適合自己的。

import pygame.camera
import time
import pygame
import cv2
import 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圖片的轉(zhuǎn)化(pygame_to_cvimage)有些地方采用cv.CreateImageHeader和SetData來實(shí)現(xiàn),注意這兩個函數(shù)在opencv3+后就消失了。因此采用numpy進(jìn)行實(shí)現(xiàn)。

至于目標(biāo)檢測,由于現(xiàn)在網(wǎng)上有很多實(shí)現(xiàn)的方法,MobileNet等等。這里我不講解具體原理,因?yàn)槲业难芯糠较虿皇沁@個,這里直接把代碼貼出來,親測成功了。

from imutils.video import FPS
import argparse
import imutils
 
 
import v4l2
import fcntl
 
import v4l2capture
import select
import image
 
import pygame.camera
import pygame
import cv2
import numpy as np
import 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()
 

上面的實(shí)現(xiàn)需要用到兩個文件,是caffe實(shí)現(xiàn)好的模型,我直接上傳(文件名為MobileNetSSD_deploy.caffemodel和MobileNetSSD_deploy.prototxt,上google能夠下載到)。

以上這篇python開啟攝像頭以及深度學(xué)習(xí)實(shí)現(xiàn)目標(biāo)檢測方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django零基礎(chǔ)入門之靜態(tài)文件的引用

    Django零基礎(chǔ)入門之靜態(tài)文件的引用

    這篇文章主要介紹了Django零基礎(chǔ)入門之靜態(tài)文件的引用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • python安裝后的目錄在哪里

    python安裝后的目錄在哪里

    在本篇內(nèi)容里小編給各位分享的是關(guān)于python安裝后的目錄位置的知識點(diǎn)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python基礎(chǔ)之定義類和對象詳解

    python基礎(chǔ)之定義類和對象詳解

    這篇文章主要為大家詳細(xì)介紹了python的定義類和對象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 使用Pandas?實(shí)現(xiàn)MySQL日期函數(shù)的解決方法

    使用Pandas?實(shí)現(xiàn)MySQL日期函數(shù)的解決方法

    這篇文章主要介紹了用Pandas?實(shí)現(xiàn)MySQL日期函數(shù)的效果,Python是很靈活的語言,達(dá)成同一個目標(biāo)或有多種途徑,我提供的只是其中一種解決方法,需要的朋友可以參考下
    2023-02-02
  • Python實(shí)現(xiàn)多線程抓取妹子圖

    Python實(shí)現(xiàn)多線程抓取妹子圖

    本文給大家匯總了3款由Python制作的多線程批量抓取美圖的代碼,主要是將獲取圖片鏈接任務(wù)和下載圖片任務(wù)用線程分開來處理了,而且這次的爬蟲不僅僅可以爬第一頁的圖片鏈接的,有類似需求的小伙伴可以參考下。
    2015-08-08
  • Python實(shí)現(xiàn)字符串匹配的KMP算法

    Python實(shí)現(xiàn)字符串匹配的KMP算法

    KMP算法的關(guān)鍵是利用匹配失敗后的信息,盡量減少模式串與主串的匹配次數(shù)以達(dá)到快速匹配的目的。這篇文章主要介紹了Python實(shí)現(xiàn)字符串匹配的KMP算法,需要的朋友可以參考下
    2019-04-04
  • Python爬蟲數(shù)據(jù)處理模塊的安裝使用教程

    Python爬蟲數(shù)據(jù)處理模塊的安裝使用教程

    這篇文章主要為大家介紹了Python爬蟲數(shù)據(jù)處理模塊的安裝使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Python實(shí)現(xiàn)動態(tài)圖解析、合成與倒放

    Python實(shí)現(xiàn)動態(tài)圖解析、合成與倒放

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)動態(tài)圖的解析、合成與倒放,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python裝飾器與線程結(jié)合提高接口訪問效率方法

    Python裝飾器與線程結(jié)合提高接口訪問效率方法

    這篇文章主要為大家介紹了如何實(shí)現(xiàn)Python裝飾器與線程結(jié)合來提高接口的訪問效率,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • pytorch __init__、forward與__call__的用法小結(jié)

    pytorch __init__、forward與__call__的用法小結(jié)

    這篇文章主要介紹了pytorch __init__、forward與__call__的用法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02

最新評論