def onIdle(self, event): """ Event to grab and display a frame from the camera. (internal use). """ if self.cap == None: #Should be cvCameraCapture instance. #unbind the idle instance, change to click. highgui.cvReleaseCapture(self.cap) #release the old instance and self.cap = highgui.cvCreateCameraCapture( self.camera) #try new one. self.displayError(self.errorBitmap, (128, 128)) raise CameraError('Unable to open camera, retrying....') event.Skip() try: img = highgui.cvQueryFrame(self.cap) except cv2.error as e: raise CameraError('Error when querying for frame: {0}'.format(e)) self._error = 0 #worked successfully img = opencv.cvGetMat(img) cv.cvCvtColor(img, img, cv.CV_BGR2RGB) if conf.as_bool(conf.config['webcam']['cropBars']): #Draw cropping region cv.cvRectangle(img, (80, -1), (560, 480), (205.0, 0.0, 0.0, 0.0), 2) self.displayImage(img) event.RequestMore()
def get_frame(self, face_rec = False): image = highgui.cvQueryFrame(self.device) face_matches = False if face_rec: grayscale = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) for cascade in self.haarfiles: matches = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) if matches: face_matches = True for i in matches: cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), cv.CV_RGB(0,0,255), 2, 5, 0) image = cv.cvGetMat(image) return (image, face_matches)
def save(self): print "Save" img = highgui.cvQueryFrame(self.cap) img = opencv.cvGetMat(img) #No BGR => RGB conversion needed for PIL output. pil = opencv.adaptors.Ipl2PIL(img) #convert to a PIL pil.save('out.jpg')
def get_image(camera): """ Grabs a frame from the camera and returns it as a PIL image """ im = highgui.cvQueryFrame(camera) im = opencv.cvGetMat(im) return opencv.adaptors.Ipl2PIL(im) #convert Ipl image to PIL image
def snapshot(arg): time.sleep(1.0) camera = highgui.cvCreateCameraCapture(0) im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) #convert Ipl image to PIL image im = opencv.adaptors.Ipl2PIL(im) im.save('test.png',"PNG")
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) #convert Ipl image to PIL image im = opencv.adaptors.Ipl2PIL(im) draw = ImageDraw.Draw(im) global speed draw.text((20,20), "FPS: " + str(speed.go()), fill=(255,255,255)) return im
def save(self, record=-1): """ Captures, crops, and saves a webcam frame. Pass an explicit record number otherwise writes to next in sequence. Returns zero-padded photo reference ID. """ img = highgui.cvQueryFrame(self.cap) img = opencv.cvGetMat(img) #No BGR => RGB conversion needed for PIL output. pil = opencv.adaptors.Ipl2PIL(img) #convert to a PIL #~ pil = pil.crop((80, 0, 560, 480)) #~ pil.show() return self.store.savePIL(pil, record)
def onCapture(self, event): self.buffer = highgui.cvQueryFrame(self.camera) self.im = highgui.cvQueryFrame(self.camera) self.im = opencv.cvGetMat(self.im) self.im = opencv.adaptors.Ipl2PIL(self.im) filename = 'img' + str(self.image_counter) + '.jpg' self.im.save('Photos/'+filename) raw = wx.Image('Photos/'+filename) # (308, 267) is the size of the picture after getting the # size of the panel bmp = raw.Rescale(308,267) bmp = bmp.ConvertToBitmap() wx.StaticBitmap(self.Photoshot_Panel, -1, bmp) self.image_counter += 1
def run(self): try: while not self.quit: frame = highgui.cvQueryFrame(self.reader) self.frame += 1 frame = opencv.cvGetMat(frame) img = adaptors.Ipl2PIL(frame) width, height = img.size zimg = zbar.Image(width, height, 'Y800', img.convert("L").tostring()) self.scanner.scan(zimg) data = None for symbol in zimg: data = symbol.data self.camlog.debug("Data is: %s" % data) self.success += 1 # handle data if not self.lastPacket == data: self.lastPacket = data try: msg = base64.b64decode(data) (rawtime, packet) = (msg[0:8], msg[8:]) ptime = struct.unpack("<d", rawtime) self.camlog.debug("Network packet! Heade (time) is %s" % (ptime,)) self.dev.write(packet) except (base64.binascii.Error, TypeError): self.camlog.error("Base64 error - could not decode packet") except struct.error: self.camlog.error("Header error - could not extract header information") else: # packet is already known, discard pass # status report to gui if self.frame % self.reportAfter == 0: self.frame = self.success = 0 squeueMutex.acquire() if self.squeue.qsize() > self.reportAfter/2: while not self.squeue.empty(): self.squeue.get() # add new status code self.squeue.put((self.frame, self.success)) squeueMutex.release() except Exception, e: exqueue.put(e) raise e
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im = opencv.cvCreateImage(opencv.cvSize(XSCALE, YSCALE), 8, 3) opencv.cvResize(im, resized_im, opencv.CV_INTER_CUBIC) #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer = ImageEnhance.Brightness(pil_img) pil_img = enhancer.enhance(BRIGHTNESS) pil_img = ImageOps.mirror(pil_img) return pil_img
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im=opencv.cvCreateImage(opencv.cvSize(XSCALE,YSCALE),8,3) opencv.cvResize( im, resized_im, opencv.CV_INTER_CUBIC); #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer=ImageEnhance.Brightness(pil_img) pil_img=enhancer.enhance(BRIGHTNESS) pil_img=ImageOps.mirror(pil_img) return pil_img
def onIdle(self, event): """ Event to grab and display a frame from the camera. (internal use). """ if self.cap == None: #Should be cvCameraCapture instance. #unbind the idle instance, change to click. highgui.cvReleaseCapture(self.cap) #release the old instance and self.cap = highgui.cvCreateCameraCapture(self.camera) #try new one. self.displayError(self.errorBitmap, (128, 128)) raise CameraError('Unable to open camera, retrying....') event.Skip() try: img = highgui.cvQueryFrame(self.cap) except cv2.error as e: raise CameraError('Error when querying for frame: {0}'.format(e)) self._error = 0 #worked successfully img = opencv.cvGetMat(img) cv.cvCvtColor(img, img, cv.CV_BGR2RGB) if conf.as_bool(conf.config['webcam']['cropBars']): #Draw cropping region cv.cvRectangle(img, (80, -1), (560, 480), (205.0, 0.0, 0.0, 0.0), 2) self.displayImage(img) event.RequestMore()
def get_image(): #Creamos camara im = highgui.cvQueryFrame(camera) im = opencv.cvGetMat(im) return opencv.adaptors.Ipl2PIL(im)
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) #convert Ipl image to PIL image return opencv.adaptors.Ipl2PIL(im)
def onIdle(self, event): img = highgui.cvQueryFrame(self.cap) img = opencv.cvGetMat(img) cv.cvCvtColor(img, img, cv.CV_BGR2RGB) self.displayImage(img) event.RequestMore()
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) #convert Ipl image to PIL image im = opencv.adaptors.Ipl2PIL(im) pix = im.load() draw = ImageDraw.Draw(im) box = 10 start = -1 xlist = [] for x in range(0, im.size[0]): color = pix[x, 0] if colorBandMatch(color) == False: pix[x, 0] = (255, 0, 0, 255) if start == -1: start = x elif start != -1: if x - start < 10: pass #print "X Axis Center Discarded; Start: ",start," End: ",x else: #print "X Axis Center Found; Start: ",start," End: ",x xlist.append((start+x)/2) pix[(start+x)/2, 0] = (0,255,0,255) start = -1 start = -1 ylist = [] for y in range(0, im.size[1]): color = pix[0, y] if colorBandMatch(color) == False: pix[0, y] = (255, 0, 0, 255) if start == -1: start = y elif start != -1: if y - start < 10: #print "Y Axis Center Discarded; Start: ",start," End: ",y pass else: #print "Y Axis Center Found; Start: ",start," End: ",y ylist.append((start+y)/2) pix[0,(start+y)/2] = (0,255,0,255) start = -1 xm = im.size[0]/2.0 ym = im.size[1]/2.0 l = 1000.0 for point in itertools.product(xlist, ylist): x = float(point[0]) #- 5# - (point[1]*0.05) - 5 y = float(point[1]) #+ (x*0.1) - 15 #print "Iterating: x: ",x," y: ", y if x-xm == 0 or y-ym == 0: print "DIVIDE BY ZERO ERROR" continue x = (y + (l/float(x-xm))*x)/((l/float(x-xm))-(float(y-ym)/l)) y = (float(y-ym)/l)*x + y x = int(x) y = int(y) print "Adjusted: x: ",x," y: ",y if x-box < 0 or x+box > im.size[0] - 1 or y-box < 0 or y+box > im.size[1] - 1: print "Out of Range" continue color = pix[x, y] print color pix[x, y] = (255, 255, 255, 255) if colorTargetMatch(color) == True: pix[x, y] = (255, 255, 0, 255) count = 0.0 for x1 in range(x-box, x+box): color = pix[x1, y-box] if colorTargetMatch(color) == True: count += 1 pix[x1, y-box] = (50, 50, 255, 255) for x2 in range(x-box, x+box): color = pix[x2, y+box] if colorTargetMatch(color) == True: count += 1 pix[x2, y+box] = (50, 50, 255, 255) for y1 in range(y-box, y+box): color = pix[x-box,y1] if colorTargetMatch(color) == True: count += 1 pix[x-box,y1] = (50, 50, 255, 255) for y2 in range(y-box, y+box): color = pix[x+box,y2] if colorTargetMatch(color) == True: count += 1 color = pix[x+box,y2] = (50, 50, 255, 255) ratio = count / (box*2*4) #draw.rectangle(((x-box, y-box),(x+box, y+box)), outline=(255,255,0)) draw.text((x+1, y+1), str(ratio), fill=(0,0,0)) #draw.rectangle(((x-box, y-box),(x+box, y+box)), outline=(0,0,0)) if ratio > 0.09 and ratio < 0.8: pix[x, y] = (255, 0, 255, 255) draw.rectangle(((x-10, y-10),(x+10, y+10)), outline=(100,255,100)) print ratio print point #yay, the point is actually on the blah return im
def get_image(dolog = False, getpix = False): global im, pix, draw im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) #convert Ipl image to PIL image im = opencv.adaptors.Ipl2PIL(im) pix = im.load() draw = ImageDraw.Draw(im) if getpix != False: global rmin, rmax, gmin, gmax, bmin, bmax cr = pix[getpix[0],getpix[1]] if cr[0] < rmin: rmin = cr[0] if cr[0] > rmax: rmax = cr[0] if cr[1] < gmin: gmin = cr[1] if cr[1] > gmax: gmax = cr[1] if cr[2] < bmin: bmin = cr[2] if cr[2] > bmax: bmax = cr[2] print "Pixel Color:",cr saveconfig() pix[xs, tl] = (100,100,255,255) pix[xs, bl] = (100,100,255,255) pix[xe, tr] = (100,100,255,255) pix[xe, br] = (100,100,255,255) xp = 0 yp = 0 wp = 0 oy = 0 for x in range(0, w): count = 0 if x % 3 > 0: continue for y in range(tr + int(ytr*x), br + int(ybr * x)): if y % 4 > 0: continue if colorTargetMatch(pix[xe-x,y]): pix[xe-x,y] = (0,255,0,255) count += 1 else: #pix[xe-x,y] = (0,0,0,255) if count >= 1: #print "x:",(xe-x),"y:",(y-(count/2)) xp = xe-x yp = y-(count/2) oy = y-count wp = count count = -1 break if count == -1: break if xp > 0 and yp - 10> 0 and xp + 10 < im.size[0]: touchcolor = pix[xp-5,yp] pix[xp,yp] = (255,255,255,255) #if colorShadowTest(draw, pix[xp+5, yp-10], pix[xp+5,yp-10], xp, yp): if colorTest(xp, yp, dolog): #if colorTest(pix[xp+5,yp],pix[xp+5,yp-10], touchcolor, xp, yp, dolog): #print "reflectoin: x:",(xe-x),"y:",(y-(count/2)) draw.rectangle(((xp-10, yp-10),(xp+10, oy+10)), outline=(100,255,100)) xd = ((xp - xs)/float(w))*640 disttop = (((tr-tl)/w) * (xp-xs)) + tl vwid = (bl-tl) + (((br-tr)-(bl-tl)) * ((xp - xs)/float(w))) yd = ((yp - disttop)/vwid) * 480 draw2.rectangle(((xd-5, yd-5),(xd+5, yd+5)), outline=(100,255,100), fill=(100,255,100)) else: #print "fayle" pix[xp+5,oy-10] = (255,255,255,255) pix[xp+5,yp] = (255,0,255,255) pix[xp-5,yp] = (0,0,255,255) if mode == "draw": return canvas elif mode == "transform": return im.transform((640,480), Image.QUAD, (xs,tl,xs,bl,xe,br,xe,tr)) else: return im
def toPIL(self): img = opencv.cvGetMat(self.image) return opencv.adaptors.Ipl2PIL(img)