def updateRobotPos():
    global cte, x, y, theta, env
    cap = WebcamVideoStream(src=int(sys.argv[1]))
    cap.start()

    print("Setting up...")
    setupImgs = []
    for i in range(15):
        frame = cap.read()
        setupImgs.append(frame)

        time.sleep(0.1)

    transformMatrix, reprojMatrix = setup(setupImgs)

    while True:
        frame = cap.read()
        #print(frame[50:60, 50:60, 1])

        x, y, theta, outImage = getRobotPosition(frame, transformMatrix)
        print(x, y, theta)
        print("")

        cte = y
        #updateImage(outImage)
        env.setRobotPose(x, y, theta)
        m = env.visualizeMap()
        #out = cv2.warpPerspective(m, reprojMatrix, (outImage.shape[1], outImage.shape[0]))
        #out = cv2.addWeighted(out, 0.5, outImage, 1 - 0.5, 0)
        updateImage(outImage)
        updateImage2(m)

        time.sleep(0.01)
def updateRobotPos():
    global cte, x, y, theta, env
    cap = WebcamVideoStream(src=int(sys.argv[1]))
    cap.start()

    print("Setting up...")
    setupImgs = []
    for i in range(15):
        frame = cap.read()
        setupImgs.append(frame)

        time.sleep(0.1)

    transformMatrix = setup(setupImgs)

    while True:
        frame = cap.read()
        #print(frame[50:60, 50:60, 1])

        x, y, theta, outImage = getRobotPosition(frame, transformMatrix)
        print(x, y, theta)
        print("")

        cte = y
        updateImage(outImage)
        env.setRobotPose(x, y, theta)
        m = env.visualizeMap()
        updateImage2(m)

        time.sleep(0.01)
class FaceAndEyeDetectorStream:
    def __init__(self, src=0):
        # initialize the video camera stream and read the first frame
        # from the stream
        self.webcam_stream = WebcamVideoStream(src).start()

        frame, frame_time = self.webcam_stream.read()

        self.frame_time = frame_time
        if frame is not None:
            (self.img, self.faces,
             self.face_features) = extract_image_features(frame)

        # initialize the variable used to indicate if the thread should
        # be stopped
        self.stopped = False

    def start(self):
        # start the thread to read frames from the video stream
        t = Thread(target=self.update, args=())
        t.daemon = True
        t.start()
        return self

    def update(self):
        # keep looping infinitely until the thread is stopped
        while True:
            # if the thread indicator variable is set, stop the thread
            #  print('updating')
            if self.stopped:
                #  print('returning')
                return

            # otherwise, read the next frame from the stream
            frame, frame_time = self.webcam_stream.read()
            self.frame_time = frame_time

            if frame is not None:
                (self.img, self.faces,
                 self.face_features) = extract_image_features(frame)
            #  print('the faces', self.faces)
            #  print('updated', self.grabbed)

    def read(self):
        # return the frame most recently read
        return (self.img, self.faces, self.face_features, self.frame_time)

    def stop(self):
        # indicate that the thread should be stopped
        self.webcam_stream.stop()
        self.stopped = True
    def _detect(self):
        """Class function to detect faces and eyes within faces"""
        video_stream = WebcamVideoStream()
        video_stream.start()
        # Cascade Classifiers
        face_cascade = cv2.CascadeClassifier(
            'haarcascades/haarcascade_frontalface_default.xml')
        eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')
        while True:
            frame = video_stream.read()
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # Detecting faces and eyes
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
                roi_gray = gray[y:y + h, x:x + h]
                roi_color = frame[y:y + h, x:x + h]

                eyes = eye_cascade.detectMultiScale(roi_gray)

                if len(eyes) / len(faces) == 2:
                    for (ex, ey, ew, eh) in eyes:
                        cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh),
                                      (0, 255, 0), 1)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                video_stream.stop()
                break

            # Display image
            cv2.imshow('Image', frame)
def main():
    vc = WebcamVideoStream(src=0).start()

    reader = easyocr.Reader(['en'])

    while True:
        # read the current camera frame
        frame = vc.read()

        # show the current frame (untouched)
        cv2.imshow("My webcam", frame)

        # if 'x' key is pressed, exit the loop
        if cv2.waitKey(1) & 0XFF == ord('x'):
            break
        # if 'c' key is pressed, process the frame for OCR
        if cv2.waitKey(1) & 0xFF == ord('c'):
            # copy the image so it will be a cleaned image for tesseract
            img = vc.mask_frame()

            # DEBUG
            img_box = draw_boxes(img, reader)

            # draw the chart containing the image with boxes
            cv2.imshow("Tesseract", img_box)

            print_img_str(img, reader)

    # close the window and de-allocate any associated memory usage
    cv2.destroyAllWindows()

    # close the already opened camera
    vc.stop()
Exemple #6
0
def updateRobotPos():
    global x, y, theta, env, outImage, started, reprojMatrix, pp, frame, transformMatrix, initFinished
    cap = WebcamVideoStream(src=int(sys.argv[1]))
    cap.start()

    print("Setting up...")
    setupImgs = []
    for i in range(15):
        frame = cap.read()
        setupImgs.append(frame)

        time.sleep(0.1)

    transformMatrix, reprojMatrix = setup(setupImgs)

    initFinished = True
    while True:
        frame = cap.read()
        x, y, theta, outImage = getRobotPosition(frame, transformMatrix)
        env.setRobotPose(x, y, theta)

        time.sleep(0.01)
def main():
    # Load Model and allocate tensors to the Coral USB device
    interpreter = tf.lite.Interpreter(
        model_path='../../../AI_Token_Recognition.tflite',
        experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
    interpreter.allocate_tensors()

    # Get input and output tensors
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    vc = WebcamVideoStream(src=0).start()

    while True:
        # read the current camera frame
        frame = vc.read()

        # show the current frame (untouched)
        cv2.imshow("My webcam", frame)

        # if 'x' key is pressed, exit the loop
        if cv2.waitKey(1) & 0XFF == ord('x'):
            break
        # if 'c' key is pressed, process the frame for OCR
        if cv2.waitKey(1) & 0xFF == ord('c'):
            img = cv2.resize(frame, (160, 160))

            cv2.imshow("Resized", img)

            # convert the frame to an array
            img_array = keras.preprocessing.image.img_to_array(img)
            img_array = tf.expand_dims(img_array, 0)

            # set the input to give it the image
            interpreter.set_tensor(input_details[0]['index'], img_array)
            interpreter.invoke()

            # get a prediction
            predictions = interpreter.get_tensor(output_details[0]['index'])
            score = tf.nn.softmax(predictions[0])

            # RESULT
            print(
                "This image most likely belongs to {} with a {:.2f} percent confidence."
                .format(np.argmax(score), 100 * np.max(score)))

    # close the window and de-allocate any associated memory usage
    cv2.destroyAllWindows()

    # close the already opened camera
    vc.stop()
Exemple #8
0
def show_camera():
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    vs = WebcamVideoStream(src=gstreamer_pipeline()).start()
    #cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    fps = FPS().start()
    take_snapshot = True

    while True:
        _ = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
        # Window
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            original_img = vs.read()
            if take_snapshot:
                save_snapshot(original_img, "original")
                take_snapshot = False
            filter = (60, 87, 120, 255, 50, 255)
            img = apply_hsv_filter(original_img, filter)
            img = erode(img, 1)
            img = dilate(img, 1)

            targets = find_contours(img)
            brColor = (255, 255, 255)
            for contour in targets:
                rr = cv2.minAreaRect(contour)
                pt = get_goal_center(rr)
                cv2.circle(original_img, pt, 6, brColor, 3)

            cv2.imshow("CSI Camera", original_img)
            # This also acts as
            keyCode = cv2.waitKey(30) & 0xFF
            # Stop the program on the ESC key
            if keyCode == 27:
                break

        fps.stop()
        print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
        print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))

        cap.release()
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")
Exemple #9
0
def main():
    model = keras.models.load_model('../../../AI_Token_Recognition')

    # Check the loaded model
    model.summary()

    vc = WebcamVideoStream(src=0).start()

    while True:
        # read the current camera frame
        frame = vc.read()

        # show the current frame (untouched)
        cv2.imshow("My webcam", frame)

        # if 'x' key is pressed, exit the loop
        if cv2.waitKey(1) & 0XFF == ord('x'):
            break
        # if 'c' key is pressed, process the frame for OCR
        if cv2.waitKey(1) & 0xFF == ord('c'):
            img = cv2.resize(frame, (160, 160))

            cv2.imshow("Resized", img)

            # convert the frame to an array
            img_array = keras.preprocessing.image.img_to_array(img)
            img_array = tf.expand_dims(img_array, 0)

            # get a prediction
            predictions = model.predict(img_array)
            score = tf.nn.softmax(predictions[0])

            # RESULT
            print(
                "This image most likely belongs to {} with a {:.2f} percent confidence."
                .format(np.argmax(score), 100 * np.max(score)))

    # close the window and de-allocate any associated memory usage
    cv2.destroyAllWindows()

    # close the already opened camera
    vc.stop()
Exemple #10
0
def main():
    # TODO; Load Model and set it to the Intel NCS2 USB

    vc = WebcamVideoStream(src=0).start()

    while True:
        # read the current camera frame
        frame = vc.read()

        # show the current frame (untouched)
        cv2.imshow("My webcam", frame)

        # if 'x' key is pressed, exit the loop
        if cv2.waitKey(1) & 0XFF == ord('x'):
            break
        # if 'c' key is pressed, process the frame for OCR
        if cv2.waitKey(1) & 0xFF == ord('c'):
            # convert the frame to an array
            img_array = keras.preprocessing.image.img_to_array(frame)
            img_array = tf.expand_dims(img_array, 0)

            # TODO: get a prediction using the NCS2

            # get a prediction
            #predictions = interpreter.get_tensor(output_details[0]['index'])
            #classes = predictions.argmax(axis=-1)
            #score = tf.nn.softmax(predictions[0])

            # RESULT
            #print(
            #    "This image most likely belongs to {} with a {:.2f} percent confidence."
            #    .format(classes[np.argmax(score)], 100 * np.max(score))
            #)

    # close the window and de-allocate any associated memory usage
    cv2.destroyAllWindows()

    # close the already opened camera
    vc.stop()
class VideoStream:
    def __init__(self,
                 src=1,
                 usePiCamera=False,
                 resolution=(320, 240),
                 framerate=32):
        # 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)

        # otherwise, we are using OpenCV so initialize the webcam
        # stream
        else:
            self.stream = WebcamVideoStream(src=src)

    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()
class App(object):
    def __init__(self):
        # self.cam = cv2.VideoCapture(0)
        # self.cam.set(3, 320)
        # self.cam.set(4, 240)
        self.cam = WebcamVideoStream(src=0, resolution=(640, 480)).start()
        self.fps = FPS().start()

        ret, self.frame = self.cam.read()

        self.conf = {
            'ColorFrameNum': 7,
            'LBPFrameNum': 7,
            'MaxFrameDiffClr': 15,
            'MaxLBPFrameUpdate': 30,
            'L_Weight': 0.3,
            'A_Weight': 0.7,
            'B_Weight': 0.7
        }

        self.ColorCheck = AdaptiveThreshold(teta=3, max_lost_cnt=1)
        self.LBPCheck = AdaptiveThreshold(teta=2, max_lost_cnt=1)

        self.ColorDistance = LABDistance()
        self.LBPDistance = LocalBinaryPatterns(
            numPoints=8,
            radius=2,
            update_prev_hist=self.conf['MaxLBPFrameUpdate'])

        self.isLost = False
        self.isLBPLost = False

        self.height, self.width = self.frame.shape[:2]

        self.kernel_e = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        self.kernel_d = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
        self.kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

        cv2.namedWindow('camshift')
        self.obj_select = RectSelector('camshift', self.onmouse)

        self.LAB_CHANNELS = (
            (24, [0, 256], "light"),  # L
            (24, [0, 256], "a"),  # a
            (24, [0, 256], "b")  # b
        )

        self.show_backproj = False
        self.track_window = None
        self.histLAB = []
        self.track_box = None

    def onmouse(self, rect):
        xmin, ymin, xmax, ymax = rect
        labRoi = self.lab[ymin:ymax, xmin:xmax]
        bgrRoi = self.frame[ymin:ymax, xmin:xmax]

        self.calcLABhist(labRoi)
        self.ColorDistance.init(bgrRoi)

        self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)
        # self.init_suspend(labRoi)
        self.isLost = False
        self.isLBPLost = False
        self.fps.reset()

    def calcLABhist(self, labRoi):
        self.histLAB = []
        for channel, param in enumerate(self.LAB_CHANNELS):
            # Init LAB histogram
            hist = cv2.calcHist([labRoi], [channel], None, [param[0]],
                                param[1])
            hist = cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
            self.histLAB.append(hist)
            # Show hist of each channel separately
            # self.show_hist(hist, param[2])

    def calcBackProjection(self):
        ch_prob = []
        ch_back_proj_prob = []

        for channel, param in enumerate(self.LAB_CHANNELS):
            prob = cv2.calcBackProject([self.lab], [channel],
                                       self.histLAB[channel], param[1], 1)
            cv2.imshow('Back projection ' + str(param[2]), prob)
            ret, prob = cv2.threshold(prob, 70, 255, cv2.THRESH_BINARY)
            cv2.imshow('Back projection thresh ' + str(param[2]), prob)
            # prob = cv2.morphologyEx(prob, cv2.MORPH_ERODE, self.kernel_e, iterations=1)
            # prob = cv2.morphologyEx(prob, cv2.MORPH_DILATE, self.kernel, iterations=1)
            prov = cv2.morphologyEx(prob,
                                    cv2.MORPH_CLOSE,
                                    self.kernel_e,
                                    iterations=2)
            ch_prob.append(prob)

        ch_back_proj_prob.append(
            cv2.addWeighted(ch_prob[0], self.conf['L_Weight'], ch_prob[1],
                            self.conf['A_Weight'], 0))

        ch_back_proj_prob.append(
            cv2.addWeighted(ch_prob[0], self.conf['L_Weight'], ch_prob[2],
                            self.conf['B_Weight'], 0))

        back_proj_prob = cv2.bitwise_and(ch_back_proj_prob[0],
                                         ch_back_proj_prob[1])
        ret, back_proj_prob = cv2.threshold(back_proj_prob, 150, 255,
                                            cv2.THRESH_BINARY)

        back_proj_prob = cv2.morphologyEx(back_proj_prob,
                                          cv2.MORPH_ERODE,
                                          self.kernel_e,
                                          iterations=1)
        back_proj_prob = cv2.morphologyEx(back_proj_prob,
                                          cv2.MORPH_DILATE,
                                          self.kernel_e,
                                          iterations=2)

        return back_proj_prob

    @staticmethod
    def show_hist(hist, channel='None'):
        bin_count = hist.shape[0]
        bin_w = 24
        img = np.zeros((256, bin_count * bin_w, 3), np.uint8)
        for i in range(bin_count):
            h = int(hist[i])
            if str(channel) == 'light':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (int(255.0 * i / bin_count), 255, 255), -1)
            elif str(channel) == 'a':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (255, int(255.0 * i / bin_count), 255), -1)
            elif str(channel) == 'b':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (255, 255, int(255.0 * i / bin_count)), -1)
            else:
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h), (255, 255, 255),
                              -1)
        img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)
        cv2.imshow('hist ' + str(channel), img)

    def camshift_algorithm(self):
        prob = self.calcBackProjection()
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        self.track_box, self.track_window = cv2.CamShift(
            prob, self.track_window, term_crit)

        if self.show_backproj:
            cv2.imshow("Back Projection", prob[..., np.newaxis])
        else:
            cv2.destroyWindow("Back Projection")

    def run(self):
        scaling_factor = 0.5
        last_frame_number = 0

        while True:
            if not self.obj_select.dragging:
                ret, self.frame = self.cam.read()
                self.frame = cv2.resize(self.frame,
                                        None,
                                        fx=scaling_factor,
                                        fy=scaling_factor,
                                        interpolation=cv2.INTER_AREA)
                self.lab = cv2.cvtColor(self.frame, cv2.COLOR_BGR2LAB)
                # self.lab = cv2.GaussianBlur(self.lab, (3,3), 0)
                kernel = np.ones((5, 5), np.float32) / 25
                self.lab = cv2.filter2D(self.lab, -1, kernel)
                self.gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)

            if ret:
                vis = self.frame.copy()

            track_window_condition = ((self.track_window)
                                      and (self.track_window[2] > 0)
                                      and (self.track_window[3] > 0))

            target_lost = self.isLost and self.isLBPLost

            # Main proccess flow
            if track_window_condition:
                if not target_lost:
                    # Apply CamShift algorithm and get new track_box
                    self.camshift_algorithm()

                    if self.fps.NumFrame % self.conf[
                            'ColorFrameNum'] == 0 and not self.isLost:
                        color_distance = self.ColorDistance.update(
                            self.frame, self.track_box)
                        self.isLost = self.ColorCheck.target_lost(
                            color_distance)
                        print("[INFO] Color track is lost:  '{}'\n".format(
                            self.isLost))
                        if self.isLost:
                            last_frame_number = self.fps.NumFrame

                    if self.fps.NumFrame % self.conf[
                            'LBPFrameNum'] == 0 and not self.isLBPLost:
                        LBP_distance = self.LBPDistance.update(
                            self.gray, self.track_window)
                        self.isLBPLost = self.LBPCheck.target_lost(
                            LBP_distance)
                        print("[INFO] LBP track is lost:  '{}'\n".format(
                            self.isLBPLost))
                        if self.isLBPLost:
                            last_frame_number = self.fps.NumFrame

                    if self.fps.NumFrame - last_frame_number >= self.conf[
                            'MaxLBPFrameUpdate']:
                        self.isLBPLost = False
                        self.isLost = False

                    try:
                        cv2.ellipse(vis, self.track_box, (0, 0, 255), 2)
                        pts = cv2.boxPoints(self.track_box)
                        pts = np.int0(pts)
                        cv2.polylines(vis, [pts], True, 255, 2)
                    except:
                        print(self.track_box)
                else:
                    cv2.putText(vis, 'Target Lost', (10, 230),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1,
                                cv2.LINE_AA)
                    # print("[INFO] Starting recovery proccess")

            elif not track_window_condition:
                cv2.putText(vis, 'Mark area of the object', (10, 230),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1,
                            cv2.LINE_AA)

            # frame processing throughput rate
            fps = self.fps.approx_compute()
            cv2.putText(vis, 'FPS {:.3f}'.format(fps), (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1,
                        cv2.LINE_AA)

            self.obj_select.draw(vis)
            cv2.imshow('camshift', vis)

            ch = 0xFF & cv2.waitKey(1)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
Exemple #13
0
	#init_UDP_client()	
	# code borrowed from Adrian
	# https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/
	cam = WebcamVideoStream(src=0).start()
  	pipeline=GripPipeline()

	cnt1 = Contour()
 	cnt2 = Contour()	
	font = cv2.FONT_HERSHEY_SIMPLEX


	fps = FPS().start()
	
	t_end = time.time() + runtime
	while time.time() < t_end:
		frame = cam.read()
		if frame is not None:
			pipeline.process(frame)
			find_target(cnt1, cnt2)
			distance, distanceRC = vision_math.find_distance(cnt1, cnt2, M_HFOV)
			angle, angleRC = vision_math.find_angle(cnt1, cnt2, M_HFOV)
			if optionImage:			
				#frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5) 
				cv2.rectangle(frame, (cnt1.x, cnt1.y), (cnt1.x + cnt1.w, cnt1.y + cnt1.h), (255, 0, 0), 2)
				cv2.rectangle(frame, (cnt2.x, cnt2.y), (cnt2.x + cnt2.w, cnt2.y + cnt2.h), (255, 0, 0), 2)
				middle = (abs(cnt1.x+cnt2.x)/2, abs(cnt1.y+cnt2.y)/2)
				if distanceRC == SUCCESS:
					cv2.putText(frame, 'D='+str(int(round(distance))), middle, font, 0.5 , (0, 0, 255), 1, cv2.LINE_4)
				if angleRC == SUCCESS:
					cv2.putText(frame, 'A='+str(int(round(angle))), (middle[0],middle[1]+30), font, 0.5 , (0, 0, 255), 1, cv2.LINE_4)
Exemple #14
0
def main():
    #Select Webcam to Stream from
    vs = WebcamVideoStream(0)
    vs.start()

    #Initialize Config for tesseract
    #tesconfigargs = ('-l digits --psm 10')
    tesconfigargs = '--oem 0 -c tessedit_char_whitelist=0123456789-. --psm 10'

    #Set pytesseract CMD (Windows only)
    pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

    #Instanciate Logger
    setup_logger('log', r'C:\Temp\ImageAnalysis.csv')
    log = logging.getLogger('log')

    log.info("-------------------------------------Capture started----------------------------------------------")

    while True:

        frame = vs.read()
        cv2.imshow('frame', frame)

        #Color to GrayScale Filter
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #small Gaussian Blur Filter to filter out grainy Stuff
        gauss = cv2.GaussianBlur(gray, (5,5),0)

        #canny detector
        canny = cv2.Canny(gauss,100,200)
        cv2.imshow('canny', canny)

        _, cnts, _= cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]


        ## loop over our contours
        screenCnt = None
        for c in cnts:
            if cv2.contourArea(c) > 1000:

                #approximate the contour
                peri = cv2.arcLength(c, True)
                approx = cv2.approxPolyDP(c, 0.02 * peri, True)

                #if our approximated contour has four points, then
                #we can assume that we have found our screen
                if len(approx) == 4:
                    screenCnt = approx
                    cv2.drawContours(frame, [screenCnt], -1, (0, 255, 0), 3)
                    x,y,width,height = cv2.boundingRect(screenCnt)
                    croppedframe = frame[y: y + height , x: x + width] # both opencv and numpy are "row-major", so y goes first

                    digit = pytesseract.image_to_string(croppedframe, config=tesconfigargs)

                    # Print and Log recognized text
                    log.info(digit)
                    break

        cv2.imshow('frame', frame)
        key = cv2.waitKey(5) & 0xFF
        if key == 27:
           break

    #Do Cleanup
    vs.stop()
    cv2.destroyAllWindows()
    # finish the authorization and grab the Dropbox client
    (accessToken, userID) = flow.finish(authCode)
    client = DropboxClient(accessToken)
    print "[SUCCESS] dropbox account linked"

# INTEGRATED WITH DROPBOX PAST THIS POINT

''' OUR MAIN LOOP WHERE I WILL USE METHODS TO RETRIEVE DIFFERENT RESULTS
    FOR ALL THE THRESHOLDING AND CASCADING.
    '''
ts = time.time()                    # Retrieve current timing.
counter = 0
while True:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    lastUploaded = datetime.datetime.now()
    #saveFrame = frame                       # For storing a copy for encoding later on.
    frame = cv2.resize(frame, (500, 500))
    counter = 0
    # At 400 width we get a real nice frames per second, increasing
    # the value will result in less frames being read per second.
    #instance = vs.getGrabbed()
    #cascades.faceCascadeDetectionOfImage(instance)
    timestamp = 0
    # So now we store the grayFrame seperately aside from our regular colored
    # frame so we can perform processing on the grayframe since many open cv
    # operations are dependent upon a grayscaled image.
    grayFrame = filters.grayScaleImage(frame)
    # May need to take this line out.
    #grayFrame = cv2.GaussianBlur(grayFrame, (21, 21), 0)
Exemple #16
0
    def sample_from_webcam(self):
        # cap = cv2.VideoCapture(0)
        vs = WebcamVideoStream(src=0).start()

        # test = cap.get(cv2.CAP_PROP_POS_MSEC)
        # ratio = cap.get(cv2.CAP_PROP_POS_AVI_RATIO)
        # frame_rate = cap.get(cv2.CAP_PROP_FPS)
        # width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        # height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        # brightness = cap.get(cv2.CAP_PROP_BRIGHTNESS)
        # contrast = cap.get(cv2.CAP_PROP_CONTRAST)
        # saturation = cap.get(cv2.CAP_PROP_SATURATION)
        # hue = cap.get(cv2.CAP_PROP_HUE)
        # gain = cap.get(cv2.CAP_PROP_GAIN)
        # exposure = cap.get(cv2.CAP_PROP_EXPOSURE)
        # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
        # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
        # cap.set(cv2.CAP_PROP_EXPOSURE, -6.0)
        # cap.set(cv2.CAP_PROP_FPS, 30)
        # cap.set(cv2.CAP_PROP_GAIN, 0)
        # cap.set(cv2.CAP_PROP_BRIGHTNESS, 64)
        # cap.set(cv2.CAP_PROP_CONTRAST, 64)
        # cap.set(cv2.CAP_PROP_SATURATION, 64)
        # print("Test: ", test)
        # print("Ratio: ", ratio)
        # print("Frame Rate: ", frame_rate)
        # print("Height: ", height)
        # print("Width: ", width)
        # print("Brightness: ", brightness)
        # print("Contrast: ", contrast)
        # print("Saturation: ", saturation)
        # print("Hue: ", hue)
        # print("Gain: ", gain)
        # print("Exposure: ", exposure)

        arduino = serial.Serial('COM3', 1000000, timeout=.1)
        time.sleep(1)
        arduino.write('r'.encode())
        time.sleep(1)

        # arduino.write("Hello from Python".encode())
        # img = cv2.imread('red.png')
        while True:
            start = time.time()

            #s, img = cap.read()
            img = vs.read()
            #cv2.resize(img, None, 0.5, 0.5, cv2.INTER_LINEAR)
            self.sample_from_image(img)

            colors = []
            for color in self.left_leds_colors:
                colors.append(int(color[2]))
                colors.append(int(color[1]))
                colors.append(int(color[0]))

            for color in self.top_leds_colors:
                colors.append(int(color[2]))
                colors.append(int(color[1]))
                colors.append(int(color[0]))

            for color in self.right_leds_colors:
                colors.append(int(color[2]))
                colors.append(int(color[1]))
                colors.append(int(color[0]))

            for color in self.bottom_led_colors:
                colors.append(int(color[2]))
                colors.append(int(color[1]))
                colors.append(int(color[0]))

            bytes = struct.pack('c' + ('B' * len(colors)) + 'c', '@'.encode(),
                                *colors, '#'.encode())
            arduino.write(bytes)

            img_with_borders = self.show_image_with_colors(img)

            # cv2.imshow("Image with Borders", img_with_borders)
            # cv2.waitKey(30)
            # time.sleep(0.1)

            end = time.time()
            elapsed = end - start
            print(str(1 / elapsed) + " FPS")
Exemple #17
0
cascades = Cascading()
# camera = cv2.VideoCapture(0)
camera = WebcamVideoStream(src=0).start() 
time.sleep(0.25)
# initialize the first frame in the video stream
# WE WILL WANT TO UPDATE THIS VARIABLE TO OFTEN CHANGE THE FIRST FRAME
# BASED ON MOVEMENT OF MOTION...WILL BE TRICKY.

cascadeTime = False

# loop over the frames of the video
while True:
    # grab the current frame and initialize the occupied/unoccupied
    # text
    
    frame = camera.read()
    #saveFrame = frame                       # For storing a copy for encoding later on.
    frame = cv2.resize(frame, (500, 500))
    #(grabbed, frame) = camera.read()
    #text = "Unoccupied"
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # So here we convert to grayscale cause color is irelevant
    gray = cv2.GaussianBlur(gray, (31, 31), 0)      # We can change the region from 21 21 later for smoothing out noise.

    
    if (cv2.waitKey(20) & 0xFF == ord('r')):    # If r is pressed we want to refresh frame to perform delta on.
        print "You pressed refresh"
        motion.refreshFirstFrame(frame)
        continue
    elif (cv2.waitKey(20) & 0xFF == ord('u')):
        print "You pressed the up arrow key"
        motion.increaseCornerDetectionThresh(100)
Exemple #18
0
class Tracker():
    def __init__(self):
        # Global Variables
        print("initialized")
        self.lc = lcm.LCM()
        lcmCameraPoseSub = self.lc.subscribe("CAMERA_POSE_CHANNEL",
                                             cameraPose_handler)
        lcmCameraPoseSub.set_queue_capacity(1)
        self.camera_pose = None
        self.vs = WebcamVideoStream(src=0).start()

    def run(self):
        state = np.matrix('0.0;0.0;0.0;0.0')  # x, y, xd, yd,

        # P and Q matrices for EKF
        P = np.matrix('10.0,0.0,0.0,0.0; \
                    0.0,10.0,0.0,0.0; \
                    0.0,0.0,10.0,0.0; \
                    0.0,0.0,0.0,10.0')

        Q = np.matrix('2.0,0.0,0.0,0.0; \
                    0.0,2.0,0.0,0.0; \
                    0.0,0.0,2.0,0.0; \
                    0.0,0.0,0.0,2.0')

        measurement = np.matrix('0;0')
        np.set_printoptions(
            formatter={'float': lambda x: "{0:0.2f}".format(x)})

        # print basic info
        print('python ' + platform.python_version())
        print('opencv ' + cv2.__version__)
        print('numpy ' + np.version.version)
        video = cv2.VideoCapture(0)
        video.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
        video.set(3, 640)
        video.set(4, 480)

        # def main():
        # open camera

        start_time = time.time()

        prev_time = time.time()
        i = 0
        counter = 0
        stop_time = time.time() + 10
        while (True):
            if time.time() > stop_time:
                break

            now_time = time.time()
            dt = now_time - prev_time

            i += 1
            counter += 1
            # run the model every 0.01 s
            if (dt > 0.005):
                prev_time = now_time

                state, P, J = run_EKF_model(state, P, Q, dt)

            # read camera
            # ret, frame = cap.read()
            # select.select((video,), (), ())
            # image_data = video.read_and_queue()
            # raw_image = np.fromstring(image_data, dtype='uint8')
            # frame = cv2.imdecode(raw_image, cv2.IMREAD_UNCHANGED)
            # frame = cv2.imdecode(np.frombuffer(image_data, dtype=np.uint8), cv2.IMREAD_COLOR)
            # ret, frame = video.read()
            frame = self.vs.read()
            print("frame: {}".format(i))
            ret == False
            if ret == True:

                # For initilization, process the whole image, otherwise, utilize the predicted position
                if i < 10:
                    mask, cimg, (x, y, r) = recognize_center_without_EKF(frame)
                else:
                    mask, cimg, (x, y,
                                 r) = recognize_center(frame, state[0],
                                                       state[1])

                # if i == 5:
                #     break
                # if x==0:
                #     continue
                measurement[0] = x
                measurement[1] = y
                if (measurement[0] != 0) and (measurement[1] != 0):
                    print("run EKF")
                    state, P = run_EKF_measurement(state, measurement, P)
                else:
                    print("no motion detected, continue")
                    # i = 0
                    # continue
                print("x: {}, state 0: {}".format(x, state[0]))
                if (x != 0):
                    cv2.circle(cimg, (int(x), int(y)), 50, (255), 5)

                if (state[0] != 0):
                    cv2.circle(cimg, (int(state[0]), int(state[1])), 20, (255),
                               3)

                msg = camera_pose_xyt_t()
                msg.x = state[0]
                msg.y = state[1]
                self.lc.publish("CAMERA_POSE_CHANNEL", msg.encode())
                # pixel_coord = np.array([state[0],state[1],1])
                # world_2d_coord = transform_camera_to_2d(pixel_coord)
                # print(world_2d_coord)
                # cv2.imshow('all',cimg)

            # close
            if cv2.waitKey(0) & 0xFF == ord('q'):
                break

        print("Time {}, frames: {}".format(time.time() - start_time, counter))
        # clean up
        video.release()
        cv2.destroyAllWindows()
class App(object):
    def __init__(self):
        # self.cam = cv2.VideoCapture(0)
        # self.cam.set(3, 320)
        # self.cam.set(4, 240)
        self.cam = WebcamVideoStream(src=0, resolution=(640, 480)).start()
        self.fps = FPS().start()

        ret, self.frame = self.cam.read()

        self.suspend_tracking = SuspendTracking(teta=3)

        self.height, self.width = self.frame.shape[:2]
        self.kernel_erode = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                                      (3, 3))
        self.kernel_dilate = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                                       (7, 7))

        cv2.namedWindow('camshift')
        self.obj_select = RectSelector('camshift', self.onmouse)

        radius = 3
        n_point = 8 * radius
        self.lbpDesc = LBP(n_point, radius)

        self.HSV_CHANNELS = (
            (24, [0, 180], "hue"),  # Hue
            (8, [0, 256], "sat"),  # Saturation
            (8, [0, 256], "val")  # Value
        )

        self.show_backproj = False
        self.track_window = None
        self.histHSV = []
        self.track_box = None

    def onmouse(self, rect):
        xmin, ymin, xmax, ymax = rect
        hsvRoi = self.hsv[ymin:ymax, xmin:xmax]

        self.calcHSVhist(hsvRoi)
        self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)
        self.init_suspend(hsvRoi)
        self.fps.reset()

    def init_suspend(self, hsvRoi):
        track_window_condition = self.track_window and self.track_window[
            2] > 0 and self.track_window[3] > 0
        if track_window_condition:
            self.camshift_algorithm()
            self.suspend_tracking.init(hsvRoi)

    def calcHSVhist(self, hsvRoi):
        self.histHSV = []
        for channel, param in enumerate(self.HSV_CHANNELS):
            # Init HSV histogram
            hist = cv2.calcHist([hsvRoi], [channel], None, [param[0]],
                                param[1])
            hist = cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
            self.histHSV.append(hist)
            # Show hist of each channel separately
            self.show_hist(hist, param[2])

    def calcBackProjection(self):
        ch_prob = []
        ch_back_proj_prob = []
        # back_proj_prob = np.ones(shape=(self.height, self.width), dtype=np.uint8) * 255
        # back_proj_prob = np.zeros(shape=(self.height, self.width), dtype=np.uint8)

        for channel, param in enumerate(self.HSV_CHANNELS):
            prob = cv2.calcBackProject([self.hsv], [channel],
                                       self.histHSV[channel], param[1], 1)
            cv2.imshow('Back projection ' + str(param[2]), prob)
            # ret, prob = cv2.threshold(prob, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            ret, prob = cv2.threshold(prob, 70, 255, cv2.THRESH_BINARY)
            cv2.imshow('Back projection thresh ' + str(param[2]), prob)
            # prob = cv2.morphologyEx(prob, cv2.MORPH_ERODE, self.kernel_erode, iterations=2)
            # prob = cv2.morphologyEx(prob, cv2.MORPH_DILATE, self.kernel_dilate, iterations=3)
            # back_proj_prob = cv2.bitwise_and(back_proj_prob, prob)
            # back_proj_prob = cv2.addWeighted(back_proj_prob, 0.4, prob, 0.6, 0)
            ch_prob.append(prob)

        ch_back_proj_prob.append(
            cv2.addWeighted(ch_prob[0], 0.6, ch_prob[1], 0.4, 0))

        ch_back_proj_prob.append(
            cv2.addWeighted(ch_prob[0], 0.6, ch_prob[2], 0.4, 0))

        back_proj_prob = cv2.bitwise_and(ch_back_proj_prob[0],
                                         ch_back_proj_prob[1])
        ret, back_proj_prob = cv2.threshold(back_proj_prob, 150, 255,
                                            cv2.THRESH_BINARY)

        back_proj_prob = cv2.morphologyEx(back_proj_prob,
                                          cv2.MORPH_ERODE,
                                          self.kernel_erode,
                                          iterations=1)
        back_proj_prob = cv2.morphologyEx(back_proj_prob,
                                          cv2.MORPH_DILATE,
                                          self.kernel_erode,
                                          iterations=2)

        return back_proj_prob

    @staticmethod
    def show_hist(hist, channel='None'):
        bin_count = hist.shape[0]
        bin_w = 24
        img = np.zeros((256, bin_count * bin_w, 3), np.uint8)
        for i in range(bin_count):
            h = int(hist[i])
            if str(channel) == 'hue':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (int(180.0 * i / bin_count), 255, 255), -1)
            elif str(channel) == 'sat':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (180, int(255.0 * i / bin_count), 255), -1)
            elif str(channel) == 'val':
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h),
                              (180, 255, int(255.0 * i / bin_count)), -1)
            else:
                cv2.rectangle(img, (i * bin_w + 2, 255),
                              ((i + 1) * bin_w - 2, 255 - h), (180, 255, 255),
                              -1)
        img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
        cv2.imshow('hist ' + str(channel), img)

    def camshift_algorithm(self):
        prob = self.calcBackProjection()
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        self.track_box, self.track_window = cv2.CamShift(
            prob, self.track_window, term_crit)

        if self.show_backproj:
            cv2.imshow("Back Projection", prob[..., np.newaxis])
        else:
            cv2.destroyWindow("Back Projection")

    def run(self):
        scaling_factor = 0.5
        while True:
            if not self.obj_select.dragging:
                ret, self.frame = self.cam.read()
                self.frame = cv2.resize(self.frame,
                                        None,
                                        fx=scaling_factor,
                                        fy=scaling_factor,
                                        interpolation=cv2.INTER_AREA)
                # blur_frame = cv2.GaussianBlur(self.frame, (21,21), 0)
                self.hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)

            if ret:
                vis = self.frame.copy()

            track_window_condition = self.track_window and self.track_window[
                2] > 0 and self.track_window[3] > 0

            if track_window_condition and not self.suspend_tracking.is_suspend(
                    self.hsv, self.track_box):
                self.camshift_algorithm()

                try:
                    cv2.ellipse(vis, self.track_box, (0, 0, 255), 2)
                    pts = cv2.boxPoints(self.track_box)
                    pts = np.int0(pts)
                    cv2.polylines(vis, [pts], True, 255, 2)
                except:
                    print(self.track_box)
            else:
                cv2.putText(vis, 'Target Lost', (10, 230),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1,
                            cv2.LINE_AA)

            # frame processing throughput rate
            fps = self.fps.approx_compute()
            # print("FPS: {:.3f}".format(fps))
            cv2.putText(vis, 'FPS {:.3f}'.format(fps), (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1,
                        cv2.LINE_AA)

            self.obj_select.draw(vis)
            cv2.imshow('camshift', vis)

            ch = 0xFF & cv2.waitKey(1)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
Exemple #20
0
	#starts streaming the camera
	stream = WebcamVideoStream(src=0).start()
  	pipeline=GripPipeline()

	#instantiates contours because we have 2 blobs in our target
	cnt1 = Contour()
 	cnt2 = Contour()	
	font = cv2.FONT_HERSHEY_SIMPLEX

	#counts how many good frames it got per second as it was running
	fps = FPS().start()
	old_counter = 0
	
	t_end = time.time() + args.runtime
	while time.time() < t_end:
		(counter, frame) = stream.read()
		if frame is not None and counter <> old_counter:
                        old_counter = counter
			pipeline.process(frame)
			rc = find_target(cnt1, cnt2)
			if rc == c.ERROR:
				tx, ty, tv = 0, 0, 0
			else:
				tx, ty = vision_math.find_tx_ty(cnt1, cnt2, camera.hfov, camera.vfov)
				tv = 1

			if args.image:			
				cv2.circle(frame, (cnt1.x , cnt1.y), 5, (255, 0, 0), 2)
				cv2.circle(frame, (cnt2.x , cnt2.y), 5, (255, 0, 0), 2)
				middle = (abs(cnt1.x+cnt2.x)/2, abs(cnt1.y+cnt2.y)/2)
				if rc == c.SUCCESS:
Exemple #21
0
# # do a bit of cleanup
# stream.release()
# cv2.destroyAllWindows()
#

# created a *threaded* video stream, allow the camera sensor to warmup,
# and start the FPS counter
print("[INFO] sampling THREADED frames from webcam...")
vs = WebcamVideoStream(src=0).start()
fps = FPS().start()

# loop over some frames...this time using the threaded stream
while fps._numFrames < num_frames:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    # frame = imutils.resize(frame, width=400)

    # check to see if the frame should be displayed to our screen
    # if args["display"] > 0:
    # cv2.imshow("Frame", frame)

    # update the FPS counter
    fps.update()
    # if cv2.waitKey(0) & 0xFF == ord('q'):
    # 	break

# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
Exemple #22
0
from WebcamVideoStream import WebcamVideoStream
import cv2
from fps import FPS

faceCascade = cv2.CascadeClassifier('faceclassifier.xml')
bodyCascade = cv2.CascadeClassifier('bodyclassifier.xml')
cam = WebcamVideoStream()
cam.startThread()
fps = FPS()

while True:

    img = cam.read()
    fps.start()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

    fps.stop()
    cv2.putText(img, 'FPS:' + str(int(fps.fps())), (450, 60),
                cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 255, 255), 2, cv2.LINE_AA)
    fps.updateFrames()
    cv2.imshow("frame", img)

    if chr(cv2.waitKey(1) & 255) == 'q':
        break

cam.stop()