我正在嘗試運行此處提供的面部跟蹤程式,但運行 python 程式回傳;
Traceback (most recent call last):
File "C:\Users\User\Desktop\Projects\Face Tracking\servo\face.py", line 27, in <module>
cv2.resizeWindow('img', 500,500)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window_w32.cpp:1473: error: (-27:Null pointer) NULL window: 'img' in function 'cvResizeWindow'
我用這個命令安裝了 OpenCV python3 -m pip install opencv-python:.
這是完整的代碼。
"""
*Face Tracking System Using Arduino - Python Code*
Close the Arduino IDE before running this code to avoid Serial conflicts.
Replace 'COM5' with the name of port where you arduino is connected.
To find the port check Arduino IDE >> Tools >> port.
Upload the Arduino code before executing this code.
# Code by Harsh Dethe, 09 Sep 2018 #
"""
import numpy as np
import serial
import time
import sys
import cv2
arduino = serial.Serial('COM3', 9600)
time.sleep(2)
print("Connection to arduino...")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
cv2.resizeWindow('img', 500,500)
cv2.line(img,(500,250),(0,250),(0,255,0),1)
cv2.line(img,(250,0),(250,500),(0,255,0),1)
cv2.circle(img, (250, 250), 5, (255, 255, 255), -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x w,y h),(0,255,0),5)
roi_gray = gray[y:y h, x:x w]
roi_color = img[y:y h, x:x w]
arr = {y:y h, x:x w}
print (arr)
print ('X :' str(x))
print ('Y :' str(y))
print ('x w :' str(x w))
print ('y h :' str(y h))
xx = int(x (x h))/2
yy = int(y (y w))/2
print (xx)
print (yy)
center = (xx,yy)
print("Center of Rectangle is :", center)
data = "X{0:d}Y{1:d}Z".format(xx, yy)
print ("output = '" data "'")
arduino.write(data)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
uj5u.com熱心網友回復:
檢查這個希望它會作業!
"""
*Face Tracking System Using Arduino - Python Code*
Close the Arduino IDE before running this code to avoid Serial conflicts.
Replace 'COM5' with the name of port where you arduino is connected.
To find the port check Arduino IDE >> Tools >> port.
Upload the Arduino code before executing this code.
# Code by Harsh Dethe, 09 Sep 2018 #
"""
import numpy as np
import serial
import time
import sys
import cv2
arduino = serial.Serial('COM3', 9600)
time.sleep(2)
print("Connection to arduino...")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
if ret == True:
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.resizeWindow('img', 500, 500)
cv2.line(img, (500, 250), (0, 250), (0, 255, 0), 1)
cv2.line(img, (250, 0), (250, 500), (0, 255, 0), 1)
cv2.circle(img, (250, 250), 5, (255, 255, 255), -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x w, y h), (0, 255, 0), 5)
roi_gray = gray[y:y h, x:x w]
roi_color = img[y:y h, x:x w]
arr = {y: y h, x: x w}
print(arr)
print('X :' str(x))
print('Y :' str(y))
print('x w :' str(x w))
print('y h :' str(y h))
xx = int(x (x h))/2
yy = int(y (y w))/2
print(xx)
print(yy)
center = (xx, yy)
print("Center of Rectangle is :", center)
data = "X{0:d}Y{1:d}Z".format(xx, yy)
print("output = '" data "'")
arduino.write(data)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
else:
break
uj5u.com熱心網友回復:
錯誤是:
error: (-27:Null pointer) NULL window: 'img' in function 'cvResizeWindow'
這意味著給定的視窗不存在(還)。
您應該首先使用createWindow或創建該視窗imshow。
視窗不是影像。包含某事物名稱的字串 ( "img") 不是事物 ( img)。如果你想resize() 一個影像,你使用了錯誤的函式。
uj5u.com熱心網友回復:
更簡單的方法來做到這一點。總是nameWindow在 while 條件塊之前使用。這是防止錯誤的實用方法。
# Naming a window
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
while 1:
ret, img = cap.read()
cv2.resizeWindow('img', 500,500)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468224.html
