一.Windows執行範例程式
1.範例程式
import cv2
import sys
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
# 逐一以影格取像
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# 在辨識的臉形外圍畫一個矩形
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 呈現影像
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 停止執行
video_capture.release()
cv2.destroyAllWindows()
2.執行結果
二.網路上的相關範例
import cv2
from webcam_gui import webcam_gui
def imgproc(frame):
# convert color to gray scale and show it
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
blur = cv2.blur(gray, (5,5))
edge = cv2.Canny(blur, 30, 100)
edge = cv2.blur(edge, (2,2))
cv2.imshow('blured edge', edge)
# convert image to black and white and show it
thresh1, thresh = cv2.threshold(edge, 60, 255, cv2.THRESH_BINARY)
cv2.imshow('thresh', thresh)
# find contours!
_,contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all the contours
cpframe = frame.copy()
cv2.drawContours(cpframe, contours, -1, (0,255,0), 3)
cv2.imshow('cpframe', cpframe)
# ================== TODO ===================
# Modify these code to suit your need
contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100]
contours = [cv2.approxPolyDP(ctr, 5 , True) for ctr in contours]
contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)]
# ============================================
# draw on the frame
cv2.drawContours(frame, contours, -1, (0,255,0), 3)
return frame
if __name__ == "__main__":
webcam_gui(imgproc, video_src=0)
1.執行結果
三.執行範例後心得
執行老師範例後,配合視訊裝置,可以呼叫出影像,並且進行人臉辨識。另外,我也在網路上查詢了其他相關範例,但程式碼更為複雜難懂,而網路上的範例是使用python2.x版本,所以必須換成python3的版本。
由於剛接觸到程式以及Opencv,所以有很多範例的程式碼是尚未瞭解的。
因此,我從Opencv的基礎語法中開始學習,包含cv2.imread(),cv2.imshow(),cv2.imwrite()等,先從如何呼叫圖片視窗,再到呼叫影片視窗。
四.目前碰到的問題
1.不知道如何學習範例程式中的辨識輪廓部分,以及辨識輪廓的數值是從何處來的。
2.有些程式中會提到Xml檔案,但不知道是如何製作出來。
五.參考的網頁資料
1.拍.電.視.時間Github範例程式:https://github.com/fatcloud/PyCV-time
2.OpenCv基礎語法學習網頁:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_table_of_contents_gui/py_table_of_contents_gui.html
3.大兵萊恩學習OpenCv路程:http://gogoprivateryan.blogspot.tw/2015/09/opencv-3-opencv-python-face-recognition.html
4.Python2 to Python3程式改變部分:https://docs.python.org/2/library/2to3.html
Comments
comments powered by Disqus