1

I am trying to run this face tracking program provided here, but running the python program returns;

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'

I installed OpenCV with this command: python3 -m pip install opencv-python.

Here is the full code.

"""
   *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
nathancy
  • 42,661
  • 14
  • 115
  • 137

3 Answers3

1

The error is:

error: (-27:Null pointer) NULL window: 'img' in function 'cvResizeWindow'

And that means the given window doesn't exist (yet).

You should have created that window first, using createWindow or imshow.

A window isn't an image. A string containing the name of something ("img") isn't the thing (img). If you wanted to resize() an image, you used the wrong function.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
1

Easier way to do this. Always used nameWindowbefore while condition block. It is practical way, to prevent an error.

# Naming a window
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
while 1:
    ret, img = cap.read()
    cv2.resizeWindow('img', 500,500)
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
0

Check this one hope it will work !

   """
   *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
Parthiban Marimuthu
  • 665
  • 1
  • 8
  • 15
  • Thank you this has fixed it but when running it, it gave [this](https://pastebin.com/PGZqtf9j) error so i changed `data = "X{0:d}Y{1:d}Z".format(xx, yy)` to `data = "X{0:f}Y{1:f}Z".format(xx, yy)` but now it gives [this](https://pastebin.com/8XvnuSMx) error –  Apr 29 '22 at 12:33
  • @M Rald. Don't try this. Too many headache for you and too many an errors. See my answer. – toyota Supra Apr 29 '22 at 15:25