Exemplo n.º 1
0
class Camera(object):
    def __init__(self):
        self.size = (640, 480)

        # initialize the camera and grab a reference to the raw camera
        # capture
        self.camera = PiCamera()
        self.camera.resolution = self.size
        self.camera.framerate = 32
        self.rawCapture = PiRGBArray(self.camera, size=self.size)

        # construct the face detector and allow the camera to warm
        # up
        self.fd = FaceDetector("cascades/haarcascade_frontalface_default.xml")
        time.sleep(0.1)

    def show_camera(self):
        # capture frames from the camera
        for f in self.camera.capture_continuous(self.rawCapture,
                                                format="bgr",
                                                use_video_port=True):
            # grab the raw NumPy array representing the image
            self.frame = f.array

            # resize the frame and convert it to grayscale
            self.frame = self.rot180(imutils.resize(self.frame, width=300))
            self.gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)

            # detect faces in the image and then clone the frame
            # so that we can draw on it
            self.faceRects = self.fd.detect(self.gray,
                                            scaleFactor=1.1,
                                            minNeighbors=5,
                                            minSize=(30, 30))
            self.frameClone = self.frame.copy()

            # loop over the face bounding boxes and draw them
            for (fX, fY, fW, fH) in self.faceRects:
                cv2.rectangle(self.frameClone, (fX, fY), (fX + fW, fY + fH),
                              (0, 255, 0), 2)

            # Desactivo el laser si detecto una cara
            if len(self.faceRects) > 0:
                GPIO.output(laser, 0)
            else:
                GPIO.output(laser, 1)

            # show our detected faces, then clear the frame in
            # preparation for the next frame
            cv2.imshow("Robopot", self.frameClone)

            self.rawCapture.truncate(0)

            # if the 'q' key is pressed, stop the loop
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break

    def rot180(self, frame):
        self.frame = frame
        return numpy.rot90(numpy.rot90(self.frame))
Exemplo n.º 2
0
# keep looping
while True:
	if (os.path.exists(faceframe)):
		fX = 0
		fY = 0
		fW = 0
		fH = 0

		# grab the current frame
		print "reading image"
		gray = cv2.imread('/dev/shm/face.jpg',0)
		height, width = gray.shape #get resolution for plotting location to screen thirds in facedetect.py
		# detect faces in the image and then clone the frame
		# so that we can draw on it
		print "making face detect rectangle"
		faceRects = fd.detect(gray, scaleFactor = 1.1, minNeighbors = 5,
			minSize = (30, 30))

		# loop over the face bounding boxes and draw them
		for (fX, fY, fW, fH) in faceRects:
			cv2.rectangle(gray, (fX, fY), (fX + fW, fY + fH), (0, 255, 0), 2)
		print "Rectangle" + str(fX) + str(fY) + str(fW) + str(fH)
		#Write rectangle to file for processing by facedetect.py
		print "writing rectangle parameters to file"
		if (os.path.exists(facelocation)):
			os.remove(facelocation)	
		with open(facelocation,'w') as f:
			f.write(str(fX)+",")
			f.write(str(fY)+",")
			f.write(str(fW)+",")
			f.write(str(fH)+",")
			f.write(str(height)+",")
Exemplo n.º 3
0
    # grab the raw NumPy array representing the image - ONLY FOR PI?
    # frame = f.array

    # resize the frame and convert it to grayscale
    frame = cv2.flip(frame, 1)
    frameorig = frame
    frame = imutils.resize(frame, width=resizeTo)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    grayorig = cv2.cvtColor(frameorig, cv2.COLOR_BGR2GRAY)
    # gray = cv2.GaussianBlur(gray, (21, 21), 0)

    # detect faces in the image and then clone the frame
    # so that we can draw on it
    faceRects = fd.detect(gray,
                          scaleFactor=1.11,
                          minNeighbors=5,
                          minSize=(minValue, minValue))
    frameClone = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)  #
    # frameClone = frame.copy() # (the original line)
    # frameClone = gray
    overlay = np.zeros((screenHeight, screenWidth, 3),
                       np.uint8)  # EXP overlay maybe for rects on fullres?

    # loop over the face bounding boxes and draw them
    for (fX, fY, fW, fH) in faceRects:
        cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 255, 0), 2)
        fWi = int(fW)
        fHi = int(fH)
        tempArea = fWi * fHi
        # print("Face area Temp is %s  " % tempArea)
        areaNumber = areaNumber + tempArea
Exemplo n.º 4
0
# Create the inverted mask for the hat
orig_mask_inv = cv2.bitwise_not(orig_mask)

# Convert hat image to BGR
# and save the original image size (used later when re-sizing the image)
hat = hat_default[:,:,0:3]

orighatHeight, origHatWidth = hat.shape[:2]

while True:
  ret, frame = cap.read()
  frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  faces = fd.detect(gray, 1.3, 5)

  for(x,y,w,h) in faces:

    hatWidth =  w + int(w * 0.2)
    hatHeight = hatWidth * origHatWidth / origHatWidth

    hat = hat_default[:,:,0:3]

    hat = cv2.resize(hat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

    y1 = y - hatHeight + int(hatHeight*.3)
    y2 = y + int(hatHeight*.3)
    x1 = x + hatWidth*0.05
    x2 = x1 + hatWidth
    cropY = 0
Exemplo n.º 5
0
while True:
	while True:
		# grab the current frame
		(grabbed, frame) = camera.read()

		# if we are viewing a video and we did not grab a
		# frame, then we have reached the end of the video
		if not grabbed:
			break

		# resize the frame and convert it to grayscale
		gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

		# detect faces in the image and then clone the frame
		# so that we can draw on it
		faceRects = fd.detect(gray, scaleFactor = 1.3, minNeighbors = 5,minSize = (100, 100))
		frameClone = frame.copy()
		face = []
		# loop over the face bounding boxes and draw them
		for (fX, fY, fW, fH) in faceRects:
			try:
				#find centroid of the rectangle
				cx = fX + int(fW/2)
				cy = fY + int(fH/2)
				fX -= int(fX/10*offset_constant)
				fY -= int(fY/10*offset_constant)
				fW += int(int(fX/10)*offset_constant) * 2

				face = frameClone[fY:fY+int((fW*proportional_w)/proportional_h),fX:fX+fW]
				#resize
				face_resized = imutils.resize(face, width = larghezza_foto)
Exemplo n.º 6
0
# Convert hat image to BGR
# and save the original image size (used later when re-sizing the image)
hat = hat_default[:, :, 0:3]

orighatHeight, origHatWidth = hat.shape[:2]

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame,
                       None,
                       fx=scaling_factor,
                       fy=scaling_factor,
                       interpolation=cv2.INTER_AREA)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = fd.detect(gray, 1.3, 5)

    for (x, y, w, h) in faces:

        hatWidth = w + int(w * 0.2)
        hatHeight = hatWidth * origHatWidth / origHatWidth

        hat = hat_default[:, :, 0:3]

        hat = cv2.resize(hat, (hatWidth, hatHeight),
                         interpolation=cv2.INTER_AREA)

        y1 = y - hatHeight + int(hatHeight * .3)
        y2 = y + int(hatHeight * .3)
        x1 = x + int(hatWidth * 0.05)
        x2 = x1 + hatWidth