1、在手機上安裝app:IP攝像頭
2、調(diào)用代碼(前提:電腦連接手機熱點)
# coding=utf-8
import cv2
import time
import os
if os.path.exists('img') == False:
os.mkdir('img')
filenames = os.listdir(r'img')
if __name__ == '__main__':
# 開啟ip攝像頭
cv2.namedWindow("camera", 1)
# 這個地址就是下面記下來的局域網(wǎng)IP
video = "http://admin:admin@172.18.34.121:8081/" # 此處@后的ipv4 地址需要修改為自己的地址
capture = cv2.VideoCapture(video)
num = len(filenames)
index = 0
imgname = -1
while True:
success, img = capture.read()
# 不進行旋轉(zhuǎn)
cv2.imshow("camera", img)
# 獲取長寬
# (h, w) = img.shape[:2]
# center = (w // 2, h // 2)
# 進行旋轉(zhuǎn)
# M = cv2.getRotationMatrix2D(center, -90, 1.0)
# rotated = cv2.warpAffine(img, M, (w, h))
# 若不關(guān)參數(shù),參數(shù)也會被旋轉(zhuǎn),影響效果
# cv2.imshow("camera", rotated)
# 按鍵處理,注意,焦點應(yīng)當在攝像頭窗口,不是在終端命令行窗口
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")
2.1、如果沒有cv2安裝包,則需要安裝opencv-python庫(功能:數(shù)字圖像處理、人臉檢測、視頻捕捉,圖像旋轉(zhuǎn)等)
python.exe -m pip install --upgrade pip install opencv-python
和poencv-contrib-python庫(功能:圖像拼接、CNN人臉識別、特征檢測、背景分割,OCR等)。注意:這兩個庫必須是同一版本。
pip install opencv-contrib-python
2.2、局域網(wǎng)IP地址的使用
?3、最后效果圖文章來源:http://www.zghlxwxcb.cn/news/detail-531223.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-531223.html
到了這里,關(guān)于Python調(diào)用手機攝像頭的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!