Exemple #1
0
def picamera_ndvi(preview=False, resolution=(640, 480), framerate=60):
    stream = PiVideoStream(resolution=resolution, framerate=framerate).start()
    time.sleep(2)
    print('Video stream started.')

    directory = 'capture_' + str(get_time_ms()) + '/'

    # loop over the frames from the video stream indefinitely
    while True:
        # grab the frame from the threaded video stream
        frame = stream.read()
        if frame is not None:
            b, g, r = cv2.split(frame)
            # get NDVI from RGB image
            ndvi = vegevision.get_ndvi(b, r)
            ndvi_colorized = apply_custom_colormap(
                255 * ndvi.astype(np.uint8),
                cmap=vegevision.load_cmap('NDVI_VGYRM-lut.csv'))
            # show the frame
            if preview:
                cv2.imshow("Video Input with NDVI", ndvi_colorized)
                print('Displaying NDVI...')
            else:
                save_image(ndvi, directory=directory)

        # if the `q` key was pressed, break from the loop
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    # do a bit of cleanup
    cv2.destroyAllWindows()
    stream.stop()
Exemple #2
0
class VideoStream:
    def __init__(self,
                 src=0,
                 isPiCamera=False,
                 resolution=(320, 240),
                 framerate=32):

        if isPiCamera:
            from pivideostream import PiVideoStream

            self.stream = PiVideoStream(resolution=resolution,
                                        framerate=framerate)

        else:
            from usbvideostream import usbVideoStream

            self.stream = usbVideoStream(src, resolution=resolution)

    def start(self):
        return self.stream.start()

    def update(self):
        self.stream.update()

    def read(self):
        return self.stream.read()

    def stop(self):
        self.stream.stop()
Exemple #3
0
class VideoStream:
    def __init__(self,
                 src=0,
                 usePiCamera=False,
                 resolution=(370, 290),
                 framerate=32):
        if usePiCamera:
            from pivideostream import PiVideoStream

            self.stream = PiVideoStream(resolution=resolution,
                                        framerate=framerate)

        else:
            self.stream() = WebcamVideoStream(src=src)

    def start(self):
        return self.stream.start()

    def update(self):
        self.stream.update()

    def read(self):
        return self.stream.read()

    def stop(self):
        self.stream.stop()
class VideoCamera(object):
    def __init__(self, flip=False, fps=10, res=(160, 128)):
        self.vs = PiVideoStream(resolution=res, framerate=fps).start()
        self.flip = flip
        print("cam init")
        time.sleep(2.0)

    def __del__(self):
        print("cam del")
        self.vs.stop()

    def flip_if_needed(self, frame):
        if self.flip:
            return np.flip(frame, 0)
        return frame

    def get_frame(self):
        frame = self.flip_if_needed(self.vs.read())
        ret, png = cv2.imencode('.png', frame)
        return png.tobytes()
Exemple #5
0
def main():
    vs = PiVideoStream()
    vs.start()
    time.sleep(2.0)
    vs.consistent()

    setup_trackbars(range_filter)
    cv2.namedWindow("Original", cv2.WINDOW_NORMAL)
    cv2.namedWindow("Thresh", cv2.WINDOW_NORMAL)

    while True:
        image = vs.read()
        frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        v1_min, v2_min, v3_min, v1_max, v2_max, v3_max = get_trackbar_values(range_filter)
        thresh = cv2.inRange(frame_to_thresh,(v1_min, v2_min, v3_min),(v1_max, v2_max, v3_max))

        cv2.imshow("Original", image)
        cv2.imshow("Thresh", thresh)

        if cv2.waitKey(1) & 0xFF is ord('q'):
            break
Exemple #6
0
class VideoStream:
    def __init__(self,
                 src=0,
                 usePiCamera=False,
                 resolution=(320, 240),
                 framerate=32,
                 **kwargs):
        # check to see if the picamera module should be used
        if usePiCamera:
            # only import the picamera packages unless we are
            # explicity told to do so -- this helps remove the
            # requirement of `picamera[array]` from desktops or
            # laptops that still want to use the `imutils` package
            from pivideostream import PiVideoStream

            # initialize the picamera stream and allow the camera
            # sensor to warmup
            self.stream = PiVideoStream(resolution=resolution,
                                        framerate=framerate,
                                        **kwargs)

        # otherwise, we are using OpenCV so initialize the webcam
        # stream

    def start(self):
        # start the threaded video stream
        return self.stream.start()

    def update(self):
        # grab the next frame from the stream
        self.stream.update()

    def read(self):
        # return the current frame
        return self.stream.read()

    def stop(self):
        # stop the thread and release any resources
        self.stream.stop()
Exemple #7
0
                       on_trackbar_lower_max_h)
    cv2.setTrackbarPos("Lower red max H:", main_window, red_lower_max_h)
    cv2.createTrackbar("Red min S:", main_window, 0, 255, on_trackbar_min_s)
    cv2.setTrackbarPos("Red min S:", main_window, red_min_s)
    cv2.createTrackbar("Red min V:", main_window, 0, 255, on_trackbar_min_v)
    cv2.setTrackbarPos("Red min V:", main_window, red_min_v)

# image process loop
while True:
    # remember the time for profiling
    if DEBUG_TIMING:
        timings = {"total_time": time.time()}
        now_time = time.time()

    # read the image from the camera
    image = camera.read()
    if image is None:
        print("No image received")
        continue

    gpioFrame = not gpioFrame
    GPIO.output(19, GPIO.HIGH if gpioFrame else GPIO.LOW)

    if DEBUG_TIMING:
        timings["camera.read"] = time.time() - now_time
        now_time = time.time()

    # TODO: ROI
    # if posX is not None:
    #     image = image[max(posY-2*radius, 0):min(posY+2*radius, height), max(posX-2*radius, 0):min(posX+2*radius, width)]
print("Model loaded")

# Init engines and hat
i2c = busio.I2C(board.SCL, board.SDA)
hat = adafruit_pca9685.PCA9685(i2c)
kit = ServoKit(channels=16)

hat.frequency = 50
kit.servo[0].angle = 100
kit.servo[1].angle = 100    

vs = PiVideoStream().start()
time.sleep(2.0)

from PIL import Image
frame = vs.read()
img = Image.fromarray(frame)
img.save("test.png")

was_direction = 0

# Starting loop
print("Ready ! press CTRL+C to START/STOP :")

try:
    
    while True:
            
            # grab the frame from the threaded video stream 
            frame = vs.read()
            image = np.array([frame]) / 255.0
Exemple #9
0
#utils.hsvWrite(130,120,80,200,190,255) #Write Networktable values Red
if (args["picamera"] > 0):
    cap = PiVideoStream().start()
else:
    cap = WebcamVideoStream().start()
time.sleep(2.0)
distanceTarget = -1
target = -1
centerX = 0
centerY = 0
r1x1 = -1
r1x2 = -1
r2x1 = -1
r2x2 = -1
while True:
    image = cap.read() #Capture frame
    #imageCopy = image

    image = imutils.resize(image, width=320) #resize - needed to allow rest of toolpath to work
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #Convert from BGR to HSV
    (lower_green,upper_green,display) = utils.hsvRead() #Get lower and Upper HSV values from the Networktable to use
    image_hsv = cv2.inRange(hsv, lower_green, upper_green) #Filter based on lower and upper HSV limits
    #blue_hsv = cv2.inRange(hsv,[80,120,80],[120,190,255]) #Filter based on lower and upper HSV limits
    #subtract Blue and Green to eliminate non green light from the path?
    #image_hsv = image_hsv - blue_hsv

    # convert the resized image to grayscale, blur it slightly,
    # and threshold it
    #gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(image_hsv, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
Exemple #10
0
window_name = 'preview'

# Creation du thread de lecture + setup
vs=PiVideoStream()
vs.camera.video_stabilization = True
# Demarrage du flux video + warmup de la camera
vs.start()
time.sleep(2.0)

# Creation de la fenetre d'affichage
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)

fps = FPS().start()

while True :

    frame = vs.read()
    fps.update()

    cv2.imshow(window_name, frame) 
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q") :
    	break

fps.stop()
print("Temps passé : {:.2f}".format(fps.elapsed()))
print("Approx. FPS : {:.2f}".format(fps.fps()))

cv2.destroyAllWindows()
vs.stop()
Exemple #11
0
def main():
    pwm = PWM(0x40)
    pwm.setPWMFreq(100)
    vs = PiVideoStream(resolution=(640, 480)).start()
    time.sleep(1.0)
    frame = vs.read()
    prvs = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    hsv = np.zeros_like(frame)
    hsv[..., 1] = 255
    mode = 0
    speed = 0
    steer = 0

    while True:
        frame = vs.read()
        #frame = rotate_image(frame)
        next = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if mode == 3:
            flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 3, 15, 3, 5,
                                                1.2, 0)
            mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
            hsv[..., 0] = ang * 180 / 3.14 / 2
            hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
            bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

        cv2.putText(frame, "Speed: {}, Steer: {}".format(speed, steer),
                    (10, 20), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 0, 0))
        if mode == 1:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if mode == 2:
            frame = cv2.Canny(frame, 20, 100)
        if mode == 3:
            cv2.imshow("Frame", bgr)
        else:
            cv2.imshow("Frame", frame)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
        if key == ord("f"):
            mode = 3
        if key == ord("e"):
            mode = 2
        if key == ord("g"):
            mode = 1
        if key == ord("c"):
            mode = 0
        if key == ord("l"):
            pwm.setPWM(0, 0, 500)
        if key == ord("r"):
            pwm.setPWM(0, 0, 300)
        if key == 81:  # left
            if steer > -1:
                steer = steer - 0.1
        if key == 83:  # right
            if steer < 1:
                steer = steer + 0.1
        if key == 82:  # up
            if speed < 1:
                speed = speed + 0.1
        if key == 84:  # down
            if steer > -1:
                speed = speed - 0.1
        if key == ord("s"):
            speed = 0
            steer = 0
        pwm.setPWM(0, 0, 500 + int(speed * 100))
        pwm.setPWM(2, 0, 670 - int(steer * 100))
        prvs = next

    cv2.destroyAllWindows()
    vs.stop()
    while True:
        # store fps
        timer_list =[]
        timer_list.append(["FPS",int(cv2.getTickFrequency()/(cv2.getTickCount() - loop_timer_start))])
        loop_timer_start = cv2.getTickCount()

        finger_list_filtered = []
        finger_num = 0
        contour_solidity = 0
        center = (0,0)
        defects_list = []
        hull_rp = np.array([])
        mean_dist_center = 0
        start_timer()
        if flag_raspberry > 0:
            frame_full = vs.read()
        else:
            ret,frame_full = vs.read()
            if ret == False:
                continue
        frame = frame_full[plotHeigth/4:plotHeigth*3/4,plotWidth/4:plotWidth*3/4]
        frame = cv2.flip(frame,-1)
        end_timer("read")

        start_timer()
        frame_hls = cv2.cvtColor(frame.copy(),cv2.COLOR_BGR2HLS)
        end_timer("cvtColor HSL")

        if calibrate_hand_flag:
            start_timer()
            roi_range = 20
class VideoCamera(object):
    def __init__(self, resolution=(320, 240), framerate=32):
        self.conf = json.load(open("conf.json"))
        self.lt = LocalTime("Baltimore")
        self.avg = None
        self.avg_count = 0
        self.motionCounter = 0
        self.motion_frames = []
        self.x = 0
        self.y = 0
        self.w = 0
        self.h = 0
        self.contour_area = 0
        self.vs = PiVideoStream(resolution, framerate).start()
        time.sleep(self.conf["camera_warmup_time"])

    def hflip(self, hflip=True):
        self.vs.hflip(hflip)

    def vflip(self, vflip=True):
        self.vs.vflip(vflip)

    def rotation(self, angle=0):
        self.vs.rotation(angle)

    def exposure_mode(self, exposure_mode="auto"):
        self.vs.exposure_mode(exposure_mode)

    def iso(self, iso=0):
        self.vs.iso(iso)

    def shutter_speed(self, speed):
        self.vs.shutter_speed(speed)

    def change_framerate(self, framerate=32):
        self.vs.stop(stop_camera=False)
        time.sleep(self.conf["camera_cooldown_time"])
        self.vs.camera.framerate = framerate
        self.vs.shutter_speed(0)
        self.vs.start()
        time.sleep(self.conf["camera_warmup_time"])
        self.avg_count = 0

    def __del__(self):
        self.vs.stop(stop_camera=True)

    def get_frame(self):
        frame = self.vs.read().copy()
        framerate = self.vs.camera.framerate
        # draw the text and timestamp on the frame
        timestamp = self.lt.now()
        ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
        cv2.putText(frame, ts, (10, frame.shape[0] - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
        cv2.putText(
            frame, "Motion on: {}; FPS: {}; Contour area: {}".format(
                self.avg_count == self.conf["camera_adjustment_frames"],
                framerate, self.contour_area), (10, 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        if self.w > 0:
            cv2.rectangle(frame, (self.x, self.y),
                          (self.x + self.w, self.y + self.h), (0, 255, 0), 2)
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()

    def get_object(self):
        frame = self.vs.read().copy()
        timestamp = self.lt.now()
        ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
        found_obj = False

        frame = imutils.resize(frame, width=500)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (21, 21), 0)

        if self.avg is None or self.avg_count < self.conf[
                "camera_adjustment_frames"]:
            self.avg = gray.copy().astype("float")
            self.avg_count += 1
            if self.avg_count == self.conf["camera_adjustment_frames"]:
                print("[INFO] motion detector live...")
            return (None, False)

        cv2.accumulateWeighted(gray, self.avg, 0.5)
        frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(self.avg))

        # threshold the delta image, dilate the thresholded image to fill
        # in holes, then find contours on thresholded image
        thresh = cv2.threshold(frameDelta, self.conf["delta_thresh"], 255,
                               cv2.THRESH_BINARY)[1]
        thresh = cv2.dilate(thresh, None, iterations=2)
        cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]

        # loop over the contours
        for c in cnts:
            # if the contour is too small, ignore it
            ca = cv2.contourArea(c)
            self.contour_area = ca
            if ca < self.conf["min_area"]:
                continue

            # compute the bounding box for the contour, draw it on the frame,
            # and update found_obj
            (self.x, self.y, self.w, self.h) = cv2.boundingRect(c)
            cv2.rectangle(frame, (self.x, self.y),
                          (self.x + self.w, self.y + self.h), (0, 255, 0), 2)
            found_obj = True

        # check to see if the room is occupied
        if found_obj:
            print("[INFO] found object!")
            # increment the motion counter
            self.motionCounter += 1
            cv2.putText(frame, ts, (10, frame.shape[0] - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
            self.motion_frames.append(frame)

            # check to see if the number of frames with consistent motion is
            # high enough
            if self.motionCounter >= self.conf["min_motion_frames"]:
                print("[INFO] occupied!")
                self.motionCounter = 0
                vis = np.concatenate(self.motion_frames, axis=0)
                return (vis, found_obj)

            return (None, False)

        # otherwise, the room is not occupied
        else:
            (self.x, self.y, self.w, self.h) = (0, 0, 0, 0)
            self.contour_area = 0
            self.motionCounter = 0
            self.motion_frames = []
            return (None, False)
Exemple #14
0
import cv2
from pivideostream import PiVideoStream
import numpy as np
import base64

addr = 'http://192.168.0.6:5000'
test_url = addr + '/api/test'

content_type = 'image/jpeg'
headers = {'content-type': content_type}

video_capture = PiVideoStream().start()

while True:
    try:
        frame = video_capture.read()
        #print(frame)
        _, img_encoded = cv2.imencode('.jpg', frame)
        response = requests.post(test_url,
                                 data=img_encoded.tostring(),
                                 headers=headers)
        str_response = json.loads(response.text)
        arr = np.fromstring(base64.b64decode(
            str_response['message']['py/b64']),
                            dtype=np.uint8)
        img = cv2.imdecode(arr, -1)
        print(img)
        cv2.imshow('frame', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    except:
class VisionSystem:
    """docstring for visionSystem"""
    def __init__(self, q1, q2):
        self.queue_MAIN_2_VS = q1
        self.queue_VS_2_STM = q2
        self.resolution = (320, 240)
        self.video_stream = PiVideoStream(self.resolution, 60)

        self.settings = {
            'disp': False,
            'dispThresh': False,
            'dispContours': False,
            'dispApproxContours': False,
            'dispVertices': False,
            'dispNames': False,
            'dispCenters': False,
            'dispTHEcenter': False,
            'erodeValue': 0,
            'lowerThresh': 40,
            'working': True,
            'autoMode': False,
            'dispGoal': True
        }

        self.prevStateDisp = self.settings['disp']
        self.prevStateDispThresh = self.settings['dispThresh']

        self.objs = []

        self.classLogger = logging.getLogger('droneNav.VisionSys')

        self.working = True
        self.t = Thread(target=self.update, args=())
        self.t.daemon = True

    def start(self):
        self.classLogger.debug('Starting vision system.')
        self.video_stream.start()
        time.sleep(2)
        self.working = True
        self.t.start()
        return

    def stop(self):
        self.working = False
        self.t.join()
        return

    def update(self):
        while 1:
            if self.working is False:
                break

            if self.queue_MAIN_2_VS.empty():
                pass
            if not self.queue_MAIN_2_VS.empty():
                self.settings = self.queue_MAIN_2_VS.get()
                self.queue_MAIN_2_VS.task_done()

            frame = self.video_stream.read()
            frame_processed = self.process_frame(frame, self.settings)
            self.detect_shapes(frame, frame_processed)

            if self.settings['disp'] is False and self.prevStateDisp is False:
                pass
            if self.settings['disp'] is True and self.prevStateDisp is False:
                cv2.namedWindow('Frame')
                key = cv2.waitKey(1) & 0xFF
                # cv2.startWindowThread()
            elif self.settings['disp'] is True and self.prevStateDisp is True:
                key = cv2.waitKey(1) & 0xFF
                cv2.imshow('Frame', frame)
            elif self.settings['disp'] is False and self.prevStateDisp is True:
                cv2.destroyWindow('Frame')

            if self.settings[
                    'dispThresh'] is False and self.prevStateDispThresh is False:
                pass
            if self.settings[
                    'dispThresh'] is True and self.prevStateDispThresh is False:
                cv2.namedWindow('Processed')
                key = cv2.waitKey(1) & 0xFF
                # cv2.startWindowThread()
            elif self.settings[
                    'dispThresh'] is True and self.prevStateDispThresh is True:
                key = cv2.waitKey(1) & 0xFF
                cv2.imshow('Processed', frame_processed)
            elif self.settings[
                    'dispThresh'] is False and self.prevStateDispThresh is True:
                cv2.destroyWindow('Processed')

            if self.settings['dispThresh'] or self.settings['disp']:
                if key == 27:
                    self.video_stream.stop()

            self.prevStateDisp = self.settings['disp']
            self.prevStateDispThresh = self.settings['dispThresh']

            # send objects to state machine
            self.queue_VS_2_STM.put(self.objs)

        cv2.destroyAllWindows()
        self.video_stream.stop()
        self.classLogger.debug('Ending vision system.')

    def process_frame(self, fr, setts):
        """ Takes frame and processes it based on settings. """
        # frame = imutils.resize(frame, width=600)
        # fr = cv2.flip(fr, 0)
        # frame = cv2.copyMakeBorder(frame, 3, 3, 3, 3,
        #                            cv2.BORDER_CONSTANT,
        #                            value=(255, 255, 255))
        frameGray = cv2.cvtColor(fr, cv2.COLOR_BGR2GRAY)
        frameBlurred = cv2.GaussianBlur(frameGray, (7, 7), 0)
        frameThresh = cv2.threshold(frameBlurred, setts['lowerThresh'], 255,
                                    cv2.THRESH_BINARY_INV)[1]
        frameThresh = cv2.erode(frameThresh,
                                None,
                                iterations=setts['erodeValue'])
        frameThresh = cv2.dilate(frameThresh,
                                 None,
                                 iterations=setts['erodeValue'])
        frameThresh = cv2.copyMakeBorder(frameThresh,
                                         3,
                                         3,
                                         3,
                                         3,
                                         cv2.BORDER_CONSTANT,
                                         value=(0, 0, 0))
        frameFinal = frameThresh

        return frameFinal

    def draw_cntrs_features(self, fr, setts, obj):
        """
        Takes frame, settings, objects list and draws features (contours,
        names, vertives, centers) on frame.
        """
        if setts['dispContours']:
            cv2.drawContours(fr, [obj['contour']], -1, (255, 255, 0), 1)
        if setts['dispApproxContours']:
            cv2.drawContours(fr, [obj['approx_cnt']], -1, (0, 255, 0), 1)
        if setts['dispNames']:
            cv2.putText(fr, obj['shape'] + str(obj['approx_cnt_area']),
                        obj['center'], cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (255, 255, 255), 1)
        if setts['dispVertices']:
            for i in range(0, len(obj['verts'])):
                cv2.circle(fr, tuple(obj['verts'][i]), 4, (255, 100, 100), 1)
        if setts['dispCenters']:
            cv2.circle(fr, (obj['center']), 2, (50, 255, 50), 1)

    def detect_shapes(self, frameOriginal, frameProcessed):
        """
        This functiion simplifies the contour, identifies shape by name,
        unpacks vertices, computes area. Then it returns a dictionary with
        all of this data.

        :param c: Contour to be approximated.
        :type c: OpenCV2 contour.
        :returns: dictionary -- shape name, vertices, approximated contour,
        approximated area.
        :rtype: dictionary.
        """

        # #####################################################################
        # FIND COUNTOURS
        # #####################################################################
        cnts = cv2.findContours(frameProcessed.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]

        # #####################################################################
        # ANALYZE CONTOURS
        # #####################################################################

        # clear list
        self.objs = []

        for index, c in enumerate(cnts):
            verts = []
            vrt = []

            # #################################################################
            # SIMPLIFY CONTOUR
            # #################################################################
            perimeter = cv2.arcLength(c, True)
            approx_cnt = cv2.approxPolyDP(c, 0.04 * perimeter, True)

            # #################################################################
            # GET CONTOUR AREA
            # #################################################################
            approx_cnt_area = cv2.contourArea(approx_cnt)

            # #################################################################
            # GETTING THE VERTICES COORDINATES
            # #################################################################
            for i in range(0, len(approx_cnt)):
                # iterate over vertices (needs additional [0]
                vrt = []
                for j in range(0, 2):
                    vrt.append(int(approx_cnt[i][0][j]))
                verts.append(vrt)

            # #################################################################
            # NAMING THE OBJECT
            # #################################################################
            # if the shape is a triangle, it will have 3 vertices
            if len(approx_cnt) == 3:
                shape = "triangle"

            # if the shape has 4 vertices, it is either a square or
            # a rectangle
            elif len(approx_cnt) == 4:
                # compute the bounding box of the contour and use the
                # bounding box to compute the aspect ratio
                (x, y, w, h) = cv2.boundingRect(approx_cnt)
                ar = w / float(h)

                # a square will have an aspect ratio that is approximately
                # equal to one, otherwise, the shape is a rectangle
                shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"

            # if the shape is a pentagon, it will have 5 vertices
            elif len(approx_cnt) == 5:
                shape = "pentagon"

            # otherwise, we assume the shape is a circle
            else:
                shape = "circle"

            # #################################################################
            # COMPUTING CENTER
            # #################################################################
            M = cv2.moments(approx_cnt)
            try:
                approx_cnt_X = int((M['m10'] / M['m00']))
                approx_cnt_Y = int((M['m01'] / M['m00']))
            except ZeroDivisionError:
                approx_cnt_X = 0
                approx_cnt_Y = 0

            obj = {
                'shape': shape,
                'verts': verts,
                'approx_cnt': approx_cnt,
                'approx_cnt_area': approx_cnt_area,
                'contour': c,
                'center': (approx_cnt_X, approx_cnt_Y)
            }

            self.objs.append(obj)

            c = c.astype('float')
            c = c.astype('int')

            self.draw_cntrs_features(frameOriginal, self.settings,
                                     self.objs[index])

        if self.settings['dispTHEcenter']:
            cv2.circle(frameOriginal,
                       (self.resolution[0] / 2, self.resolution[1] / 2), 2,
                       (50, 50, 255), 1)

        if self.settings['dispGoal'] and bool(self.objs):
            cv2.line(frameOriginal,
                     (self.resolution[0] / 2, self.resolution[1] / 2),
                     self.objs[0]['center'], (255, 0, 0), 2)
Exemple #16
0
import time

# initialize variables
microsleep = False          # boolean to store subject state, default
counter = 0                 # variable to count instances of microsleep

# initialize the video stream specs, allow time for camera sensor to warmup
vs = PiVideoStream(resolution=(320, 240),framerate=32)
vs.start()
time.sleep(2.0)

# initialize start time after warmup
start_time = time.time()

while(True):
    frame = vs.read()                                               # start parsing through frames
    frame = imutils.resize(frame, width=400)                        # resize each individual frames

    onset = time.time()                                             # declare time of EC onset
    
    roi = frame[100:300, 100:250]                                   # crop to region of interest
    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)                    # convert BGR frame to grayscale
    ret, thresh_img = cv2.threshold(gray,200,255,cv2.THRESH_BINARY) # create binary (BW) mask of grayscale image

    clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(4,4))      # perform adaptive histogram equalization
    hist = clahe.apply(gray)                                        # apply histogram to grayscale frame
    blur = cv2.bilateralFilter(hist,9,75,75)                        # noise removal, keep edges intact
    edges = cv2.Canny(blur,150,220)                                 # apply canny edge detection algorithm

    status = "eyes closed"                                          # declare current status, default is closed