Help- OpenCV, face recognition - cannot open display - Raspberry Pi Forums
hello everybody,
use ssh pc pi , use webcam camera c270. want recognize face, have stuff :
(video:3718): gtk-warning **cannot open display : : 1.0
here code :
import sys
import cv2.cv cv
optparse import optionparser
min_size = (20, 20)
image_scale = 2
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0
def detect_and_draw(img, cascade):
# allocate temporary images
gray = cv.createimage((img.width,img.height), 8, 1)
small_img = cv.createimage((cv.round(img.width / image_scale),cv.round (img.height / image_scale)), 8, 1)
# convert color input image grayscale
cv.cvtcolor(img, gray, cv.cv_bgr2gray)
# scale input image faster processing
cv.resize(gray, small_img, cv.cv_inter_linear),
cv.equalizehist(small_img, small_img)
if(cascade):
t = cv.gettickcount()
faces = cv.haardetectobjects(small_img, cascade, cv.creatememstorage(0),haar_scale, min_neighbors, haar_flags, min_size)
t = cv.gettickcount() - t
print "time taken detection = %gms" % (t/(cv.gettickfrequency()*1000.))
if faces:
((x, y, w, h), n) in faces:
# input cv.haardetectobjects resized, scale the
# bounding box of each face , convert 2 cvpoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.rectangle(img, pt1, pt2, cv.rgb(255, 0, 0), 3, 8, 0)
cv.showimage("video", img)
if __name__ == '__main__':
parser = optionparser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()
cascade = cv.load(options.cascade)
if len(args) != 1:
parser.print_help()
sys.exit(1)
input_name = args[0]
if input_name.isdigit():
capture = cv.createcameracapture(int(input_name))
else:
capture = none
cv.namedwindow("video", 1)
#size of video
width = 160
height = 120
if width none:
width = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_width))
else:
cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_width,width)
if height none:
height = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_height))
else:
cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_height,height)
if capture:
frame_copy = none
while true:
frame = cv.queryframe(capture)
if not frame:
cv.waitkey(0)
break
if not frame_copy:
frame_copy = cv.createimage((frame.width,frame.height),
cv.ipl_depth_8u, frame.nchannels)
if frame.origin == cv.ipl_origin_tl:
cv.copy(frame, frame_copy)
else:
cv.flip(frame, frame_copy, 0)
detect_and_draw(frame_copy, cascade)
if cv.waitkey(10) >= 0:
break
else:
image = cv.loadimage(input_name, 1)
detect_and_draw(image, cascade)
cv.waitkey(0)
cv.destroywindow("video")
please me fix it, lot
use ssh pc pi , use webcam camera c270. want recognize face, have stuff :
(video:3718): gtk-warning **cannot open display : : 1.0
here code :
import sys
import cv2.cv cv
optparse import optionparser
min_size = (20, 20)
image_scale = 2
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0
def detect_and_draw(img, cascade):
# allocate temporary images
gray = cv.createimage((img.width,img.height), 8, 1)
small_img = cv.createimage((cv.round(img.width / image_scale),cv.round (img.height / image_scale)), 8, 1)
# convert color input image grayscale
cv.cvtcolor(img, gray, cv.cv_bgr2gray)
# scale input image faster processing
cv.resize(gray, small_img, cv.cv_inter_linear),
cv.equalizehist(small_img, small_img)
if(cascade):
t = cv.gettickcount()
faces = cv.haardetectobjects(small_img, cascade, cv.creatememstorage(0),haar_scale, min_neighbors, haar_flags, min_size)
t = cv.gettickcount() - t
print "time taken detection = %gms" % (t/(cv.gettickfrequency()*1000.))
if faces:
((x, y, w, h), n) in faces:
# input cv.haardetectobjects resized, scale the
# bounding box of each face , convert 2 cvpoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.rectangle(img, pt1, pt2, cv.rgb(255, 0, 0), 3, 8, 0)
cv.showimage("video", img)
if __name__ == '__main__':
parser = optionparser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()
cascade = cv.load(options.cascade)
if len(args) != 1:
parser.print_help()
sys.exit(1)
input_name = args[0]
if input_name.isdigit():
capture = cv.createcameracapture(int(input_name))
else:
capture = none
cv.namedwindow("video", 1)
#size of video
width = 160
height = 120
if width none:
width = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_width))
else:
cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_width,width)
if height none:
height = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_height))
else:
cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_height,height)
if capture:
frame_copy = none
while true:
frame = cv.queryframe(capture)
if not frame:
cv.waitkey(0)
break
if not frame_copy:
frame_copy = cv.createimage((frame.width,frame.height),
cv.ipl_depth_8u, frame.nchannels)
if frame.origin == cv.ipl_origin_tl:
cv.copy(frame, frame_copy)
else:
cv.flip(frame, frame_copy, 0)
detect_and_draw(frame_copy, cascade)
if cv.waitkey(10) >= 0:
break
else:
image = cv.loadimage(input_name, 1)
detect_and_draw(image, cascade)
cv.waitkey(0)
cv.destroywindow("video")
please me fix it, lot
first, if use \{code\} tags here in forum, code gets lot easier read.vantruong57 wrote:hello everybody,
use ssh pc pi , use webcam camera c270. want recognize face, have stuff :
(video:3718): gtk-warning **cannot open display : : 1.0
please me fix it, lot
gtk- warning see either:
- because havent started x-windows , program want access x-server (desktop).
- or, desktop started local display environment variable in ssh session not pointing that. can fixed simple "export display=:0.0" likely.
code: select all
import sys import cv2.cv cv optparse import optionparser min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 def detect_and_draw(img, cascade): # allocate temporary images gray = cv.createimage((img.width,img.height), 8, 1) small_img = cv.createimage((cv.round(img.width / image_scale),cv.round (img.height / image_scale)), 8, 1) # convert color input image grayscale cv.cvtcolor(img, gray, cv.cv_bgr2gray) # scale input image faster processing cv.resize(gray, small_img, cv.cv_inter_linear), cv.equalizehist(small_img, small_img) if(cascade): t = cv.gettickcount() faces = cv.haardetectobjects(small_img, cascade, cv.creatememstorage(0),haar_scale, min_neighbors, haar_flags, min_size) t = cv.gettickcount() - t print "time taken detection = %gms" % (t/(cv.gettickfrequency()*1000.)) if faces: ((x, y, w, h), n) in faces: # input cv.haardetectobjects resized, scale # bounding box of each face , convert 2 cvpoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.rectangle(img, pt1, pt2, cv.rgb(255, 0, 0), 3, 8, 0) cv.showimage("video", img) if __name__ == '__main__': parser = optionparser(usage = "usage: %prog [options] [filename|camera_index]") parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml") (options, args) = parser.parse_args() cascade = cv.load(options.cascade) if len(args) != 1: parser.print_help() sys.exit(1) input_name = args[0] if input_name.isdigit(): capture = cv.createcameracapture(int(input_name)) else: capture = none cv.namedwindow("video", 1) #size of video width = 160 height = 120 if width none: width = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_width)) else: cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_width,width) if height none: height = int(cv.getcaptureproperty(capture, cv.cv_cap_prop_frame_height)) else: cv.setcaptureproperty(capture,cv.cv_cap_prop_frame_height,height) if capture: frame_copy = none while true: frame = cv.queryframe(capture) if not frame: cv.waitkey(0) break if not frame_copy: frame_copy = cv.createimage((frame.width,frame.height), cv.ipl_depth_8u, frame.nchannels) if frame.origin == cv.ipl_origin_tl: cv.copy(frame, frame_copy) else: cv.flip(frame, frame_copy, 0) detect_and_draw(frame_copy, cascade) if cv.waitkey(10) >= 0: break else: image = cv.loadimage(input_name, 1) detect_and_draw(image, cascade) cv.waitkey(0) cv.destroywindow("video")
raspberrypi
Comments
Post a Comment