打开 cv3.1,python3.5
open cv3.1, python 3.5
我正在尝试使用 opencv3 和 python3 进行人脸识别:
这是我的代码:
import errno
import numpy as np
import cv2
import os
import sys
import errno
os.chdir("E:\work\data")
face=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def read_images(path):
x,y=[],[]
c=0
for (dir,dirs,files) in os.walk(path):
for folders in dirs:
spath=os.path.join(dir,folders)
print(spath)
for file in os.listdir(spath):
fpath=os.path.join(spath,file)
img=cv2.imread(fpath,0)
x.append(np.asarray(img,dtype=np.uint8))
y.append(c)
c=c+1
return [x,y]
def face_rec():
names=['Eldhose','Peter']
read_images("E:\work\data")
y=np.asarray(y,dtype=np.int32)
model=cv2.face.createEigenFaceRecognizer()
model.train(np.asarray(x),np.asarray(y))
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while True:
suc,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for (a,b,c,d) in faces:
cv2.rectangle(frame,(a,b),(a+c,b+d),(255,0,0),2)
roi=gray[b:b+d,a:a+c]
roi=cv2.resize(roi,(200,200),interpolation=cv2.INTER_LINEAR)
params=model.predict(roi)
print("Label:%s,confidence %.2f" %(params[0],params[1])
cv2.imshow("camera",frame)
cv2.waitKey(30) & 0xff
face_rec()
cv2.destroyAllWindows()
显示错误。我是初学者。我基础薄弱。
其中一个错误是 cv2.imshow("camera",frame)
语法错误:语法无效
请帮我 。
提前致谢:)
`
OpenCV_test1.py
此程序打开相同目录名称中的文件"image.jpg"并显示原始图像和原始图像的 Canny 边缘
导入cv2
将 numpy 导入为 np
导入 os
#
def main():
imgOriginal = cv2.imread("image.jpg") # 打开图片
if imgOriginal is None: # if image was not read successfully
print "error: image not read from file \n\n" # print error message to std out
os.system("pause") # pause so user can see error message
return # and exit function (which exits program)
# end if
imgGrayscale = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY) # convert to grayscale
imgBlurred = cv2.GaussianBlur(imgGrayscale, (5, 5), 0) # blur
imgCanny = cv2.Canny(imgBlurred, 100, 200) # get Canny edges
cv2.namedWindow("imgOriginal", cv2.WINDOW_AUTOSIZE) # create windows, use WINDOW_AUTOSIZE for a fixed window size
cv2.namedWindow("imgCanny", cv2.WINDOW_AUTOSIZE) # or use WINDOW_NORMAL to allow window resizing
cv2.imshow("imgOriginal", imgOriginal) # show windows
cv2.imshow("imgCanny", imgCanny)
cv2.waitKey() # hold windows open until user presses a key
cv2.destroyAllWindows() # remove windows from memory
return
#
if name == "main":
主()
python OpenCV_test1.py
文件 "OpenCV_test1.py",第 4 行
import cv2 import numpy as np import os
我也有同样的错误
我正在尝试使用 opencv3 和 python3 进行人脸识别: 这是我的代码:
import errno
import numpy as np
import cv2
import os
import sys
import errno
os.chdir("E:\work\data")
face=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def read_images(path):
x,y=[],[]
c=0
for (dir,dirs,files) in os.walk(path):
for folders in dirs:
spath=os.path.join(dir,folders)
print(spath)
for file in os.listdir(spath):
fpath=os.path.join(spath,file)
img=cv2.imread(fpath,0)
x.append(np.asarray(img,dtype=np.uint8))
y.append(c)
c=c+1
return [x,y]
def face_rec():
names=['Eldhose','Peter']
read_images("E:\work\data")
y=np.asarray(y,dtype=np.int32)
model=cv2.face.createEigenFaceRecognizer()
model.train(np.asarray(x),np.asarray(y))
cap=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while True:
suc,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,5)
for (a,b,c,d) in faces:
cv2.rectangle(frame,(a,b),(a+c,b+d),(255,0,0),2)
roi=gray[b:b+d,a:a+c]
roi=cv2.resize(roi,(200,200),interpolation=cv2.INTER_LINEAR)
params=model.predict(roi)
print("Label:%s,confidence %.2f" %(params[0],params[1])
cv2.imshow("camera",frame)
cv2.waitKey(30) & 0xff
face_rec()
cv2.destroyAllWindows()
显示错误。我是初学者。我基础薄弱。 其中一个错误是 cv2.imshow("camera",frame) 语法错误:语法无效 请帮我 。 提前致谢:)
`
OpenCV_test1.py
此程序打开相同目录名称中的文件"image.jpg"并显示原始图像和原始图像的 Canny 边缘
导入cv2 将 numpy 导入为 np 导入 os
#def main(): imgOriginal = cv2.imread("image.jpg") # 打开图片
if imgOriginal is None: # if image was not read successfully
print "error: image not read from file \n\n" # print error message to std out
os.system("pause") # pause so user can see error message
return # and exit function (which exits program)
# end if
imgGrayscale = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY) # convert to grayscale
imgBlurred = cv2.GaussianBlur(imgGrayscale, (5, 5), 0) # blur
imgCanny = cv2.Canny(imgBlurred, 100, 200) # get Canny edges
cv2.namedWindow("imgOriginal", cv2.WINDOW_AUTOSIZE) # create windows, use WINDOW_AUTOSIZE for a fixed window size
cv2.namedWindow("imgCanny", cv2.WINDOW_AUTOSIZE) # or use WINDOW_NORMAL to allow window resizing
cv2.imshow("imgOriginal", imgOriginal) # show windows
cv2.imshow("imgCanny", imgCanny)
cv2.waitKey() # hold windows open until user presses a key
cv2.destroyAllWindows() # remove windows from memory
return
#
if name == "main": 主()
python OpenCV_test1.py 文件 "OpenCV_test1.py",第 4 行 import cv2 import numpy as np import os
我也有同样的错误