Python Forum
python variable issues - using spyder and opencv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python variable issues - using spyder and opencv
#1
Hello all! thank you for reading and hopefully helping with my issue.

trying to run OpenCV through python spyder on anaconda. I am following an online course where they have created a virtual environment so that everything that is required should be there. I have also run it through my own anaconda OpenCV spyder install (I've tried asking the creator but no response from them Sad )

I've run the following code that they created which is below and then I got an error which I can't seem to find online either (error code also below)

I've been trying to alter different parts to see what I can sus out and I think it has to do with some variables not being created as variables which is strange. I've tried creating other variables which show up in spyders variable explore but these particular two just don't seem to be created. they are as follows:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

I've set up the working directory file containing all the files including the cascade files etc.

I'm very stuck on what is causing this so I'm hoping someone in the community has encountered.

I'm also well aware that this may be normal and its something completely different going wrong and I am just beginning my python journey.

anyway thank you to those that can offer advice.

the code:

# Face Recognition

# Importing the libraries
import cv2

# Loading the cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

# Defining a function that will do the detections
def detect(gray, frame):
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    return frame

# Doing some Face Recognition with the webcam
video_capture = cv2.VideoCapture(0)
while True:
    _, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    canvas = detect(gray, frame)
    cv2.imshow('Video', canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
the kernnel with error at bottom:
import cv2

# Loading the cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

# Defining a function that will do the detections
def detect(gray, frame):
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
    return frame


# Doing some Face Recognition with the webcam
video_capture = cv2.VideoCapture(0)
while True:
    _, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    canvas = detect(gray, frame)
    cv2.imshow('Video', canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()
Error:
Traceback (most recent call last): File "<ipython-input-1-806d0e67c07b>", line 25, in <module> canvas = detect(gray, frame) File "<ipython-input-1-806d0e67c07b>", line 9, in detect faces = face_cascade.detectMultiScale(gray, 1.3, 5) error: /Users/jenkins/miniconda/1/x64/conda-bld/conda_1486587097465/work/opencv-3.1.0/modules/objdetect/src/cascadedetect.cpp:1639: error: (-215) !empty() in function detectMultiScale
Reply
#2
Please, try these steps:
1 - Use the full path of each file: eg. 'C:/MyOpenCVProject/haarcascade_frontalface_default.xml' (yes, with '/')
2 - Check if the files are really a valid xml files. I tested with:
3 - Check if the output of load() is True:
face_cascade = cv2.CascadeClassifier()
ret = face_cascade.load('C:/MyOpenCVProject/haarcascade_frontalface_default.xml')
print(ret)
Reply
#3
Hello,

So I input and changed what you suggested, however print(rep) came up FALSE

any ideas what that would suggest?
Reply
#4
Did you check the contents of the XML files?
Are they really the same as the ones I posted?
First line must be:
<?xml version="1.0"?>
Reply
#5
ok so funny story, I finally figured out what I was doing wrong and turns out i was being a complete noob! turns out I thought that a certain button was 'save this current location as the WD'. turns out it actually returns the WD to its original location (which was not the file I was trying to set).

glad to say that it is all working correctly now. thank you very much Gontajones for your time spent trying to help me.

this whole experience was actually pretty demoralizing and frustrating and I nearly gave up what I was trying but glad I persevered! all this experience will hopefully help in the future!

thanks again!
Reply
#6
(Jun-19-2018, 09:30 AM)Afrodizzyjack Wrote: ok so funny story, I finally figured out what I was doing wrong and turns out i was being a complete noob! turns out I thought that a certain button was 'save this current location as the WD'. turns out it actually returns the WD to its original location (which was not the file I was trying to set).

glad to say that it is all working correctly now. thank you very much Gontajones for your time spent trying to help me.

this whole experience was actually pretty demoralizing and frustrating and I nearly gave up what I was trying but glad I persevered! all this experience will hopefully help in the future!

thanks again!

That's nice to hear!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Explorer in spyder driesdep 1 203 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Problem with using opencv in python Raunak1023984765 21 1,678 Feb-21-2024, 04:25 PM
Last Post: Pedroski55
Shocked Issues Installing Pyenv/Python 3.9.1 Brandon_Contactum 1 2,518 Feb-22-2022, 06:32 PM
Last Post: snippsat
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,280 Dec-23-2021, 12:34 PM
Last Post: snippsat
  Python OpenCV window not opening in fullscreen mode Zman350x 0 3,292 Apr-29-2021, 07:54 PM
Last Post: Zman350x
  How can I make a short-key in Spyder (Python IDE)? moose 3 2,673 Nov-02-2020, 12:13 PM
Last Post: jefsummers
  How can I scroll over my data points when creating plots in Python? (I'm using Spyder moose 0 1,603 Nov-02-2020, 07:18 AM
Last Post: moose
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,339 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  Python coding in Spyder IDE fl4m3 0 2,438 Sep-26-2020, 07:03 PM
Last Post: fl4m3
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,208 Sep-18-2020, 02:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020