Shideh Shideh - 1 year ago 420
Python Question

Printing text on image in OpenCV Python

I have made a dictionary,done some calculations and returned some values. Now i'm trying to print those returned values on an image, but I get this error:

expected string or Unicode object, int found

let's say my returned value is:

(58, 47, 88.0)

and I'm using the following to display it on a screen:

cv2.putText(img, T ,(619,351), font, 1,(0,0,255),2,cv2.LINE_AA)

Also, I want to be able to display on the image:
T = (58, 47, 88.0)

Answer Source

You seem to be going in right direction I guess, The code looks good enough, But there is a TypeError which indicates that you have passed an Integer in place of a string and most probably, that mistake must be somewhere in the value of T, Check if it's of type int , you can always check the type of any variable using type(T), If it returns int then you probably need to typecast the variable as str(T) so the final code may look like:

if not type(T) is str:
        cv2.putText(img, str(T) ,(619,351), font, 1,(0,0,255),2,cv2.LINE_AA)
    except Exception,e:
        print e
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download