Choosing which image to take as input with a dialogue box.

asked 2018-07-18 12:47:08 -0600

Philia gravatar image

updated 2018-07-18 12:47:32 -0600

So, I am trying to browse through a directory and choose which image I want to take as input. My code:

import numpy as np
import cv2 as cv
import easygui

face_cascade = cv.CascadeClassifier('face.xml')
def todo():
    img = easygui.fileopenbox()
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 7, 7)
    for (x,y,w,h) in faces:
        cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]


        cv.imshow('img',img)
        cv.waitKey(0)
        cv.destroyAllWindows()

todo()

When I input an image I get error

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
TypeError: src is not a numpy array, neither a scalar
edit retag flag offensive close merge delete

Comments

what do you think, easygui.fileopenbox() returns ?

(hint: it's probably NOT an image)

berak gravatar imageberak ( 2018-07-18 12:56:33 -0600 )edit

next: put cv.destroyAllWindows() OUT of the loop

sorry to say so, but you have to understand, what you're doing here, with every single line of your code.

simply copy/pasting things won't get you anywhere.

berak gravatar imageberak ( 2018-07-18 13:24:10 -0600 )edit

Have a look at http://easygui.sourceforge.net/api.html --> you need to use the correct set of parameters on the fileboxopen() function. However this is totally unrelated to OpenCV ...

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-24 04:36:00 -0600 )edit

uni_code=easygui.fileopenbox()

img_path = unicodedata.normalize('NFKD',uni_code).encode('ascii','ignore')

img = cv2.imread(img_path,1)

Added this. Still get error

img = cv2.imread(img_path,1)
TypeError: bad argument type for built-in operation
Philia gravatar imagePhilia ( 2018-07-24 13:41:03 -0600 )edit

@Philia to me that error clearly states that your generated img_path is incorrect and not in the string format, as imread is expecting. You have to supply the correct input type.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-25 02:34:24 -0600 )edit