Ejemplo n.º 1
0
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
roiBox = None

# Main loop
while (1):
    ret, frame = cap.read()

    if roiBox is not None:
        # Making the frame into HSV and backproject the HSV frame
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        # Apply meanshift to get the new location
        ret, roiBox = cv2.CamShift(dst, roiBox, term_crit)

        # Draw it on image
        pts = cv2.boxPoints(ret)
        pts = np.intp(pts)
        cv2.polylines(frame, [pts], True, 255, 2)

        # Draw the center
        cx = (pts[0][0] + pts[1][0]) // 2
        cy = (pts[0][1] + pts[2][1]) // 2
        cv2.circle(frame, (cx, cy), 4, (0, 255, 0), 2)
        # cv2.imshow('img2',frame)

    # handle if the 'i' key is pressed, then go into ROI
    # selection mode
    cv2.imshow("image", frame)
Ejemplo n.º 2
0
        vis = copy
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # convert to HSV
        mask = cv2.inRange(hsv, np.array(
            (0., 60., 32.)), np.array(
                (180., 255.,
                 255.)))  # eliminate low and high saturation and value values

        # The next line shows which pixels are being used to make the histogram.
        # it sets to black all the ones that are masked away for being too over or under-saturated
        if showHistMask:
            vis[mask == 0] = 0

        prob = cv2.calcBackProject([hsv], [0], hist, [0, 180], 1)
        prob &= mask
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        track_box, track_window = cv2.CamShift(prob, track_window, term_crit)

        print("Track box:", track_box)

        # Extract track_box[1]. track_box[1] contains the minor and major axes of the detected object (In my case, it is a tennis ball.)
        tuple = track_box[1]
        # tuple[1] seems to be the major axis. I wrote a boolean statement to see if the major axis is smaller than 100.
        # If I put the tennis ball at the distance about 0.5 meters from the camera, the major axis of one tennis ball will be within 100.
        # However, the major axis of two tennis ball will exceed 100.
        # Therefore, I want to use this method to solve the problem that two tennis balls are circled together even if they are separated.
        flag = tuple[1] < 100
        # I print out the result.
        print(tuple[1])
        print(flag)
        # However, I found that even if flag is False, the "if statement" still works and draw an ellipse. I don't know why this happens.
        if showBackProj and flag:
Ejemplo n.º 3
0
def cam_shift(frame):

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
    ret, track_window = cv2.CamShift(dst, track_window, termination)
Ejemplo n.º 4
0
    def run(self, frame):
        global Update
        global MorphOps
        global Channel
        global Realtime
        y = None
        if True:

            self.frame = frame.copy()
            vis = self.frame.copy()
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))
            mask = cv2.inRange(hsv, np.array((0., 0., 0.)),
                               np.array((180., 255., 255.)))
            if self.selection:
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1 - x0, y1 - y0)
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]

                if Channel:
                    hist = cv2.calcHist([hsv_roi], [0, 1], mask_roi, [16, 5],
                                        [0, 180, 0, 256])
                else:
                    hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16],
                                        [0, 180])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 2:
                if Channel:
                    prob = cv2.calcBackProject([hsv], [0, 1], self.hist,
                                               [0, 180, 0, 256], 1)
                else:
                    prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180],
                                               1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                self.previous_window = self.track_window
                kernel = np.ones((5, 5), np.uint8)
                if MorphOps:
                    prob = cv2.morphologyEx(prob, cv2.MORPH_OPEN, kernel)
                    prob = cv2.morphologyEx(prob, cv2.MORPH_CLOSE, kernel)
                prob = cv2.GaussianBlur(prob, (5, 5), 0)

                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)
                if get_window_size(self.track_window) <= size_treshold:
                    self.track_window = get_increased_window(
                        self.previous_window)
                    self.tracking_state = 2
                else:
                    self.tracking_state = 1
                font = cv2.FONT_HERSHEY_SIMPLEX
                # print "Target Missing."
                cv2.putText(vis, 'Target Missing', (10, 400), font, 1,
                            (255, 255, 255), 2, 1)

            if self.tracking_state == 1:
                self.selection = None
                if Channel:
                    prob = cv2.calcBackProject([hsv], [0, 1], self.hist,
                                               [0, 180, 0, 256], 1)
                else:
                    prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180],
                                               1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                self.previous_window = self.track_window
                kernel = np.ones((5, 5), np.uint8)
                if MorphOps:
                    prob = cv2.morphologyEx(prob, cv2.MORPH_OPEN, kernel)
                    prob = cv2.morphologyEx(prob, cv2.MORPH_CLOSE, kernel)
                prob = cv2.GaussianBlur(prob, (5, 5), 0)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)
                if get_window_size(self.track_window) <= size_treshold:
                    self.track_window = get_increased_window(
                        self.previous_window)
                    self.tracking_state = 2
                if self.show_backproj:
                    vis[:] = prob[..., np.newaxis]
                xx0, yy0, xx1, yy1 = self.track_window
                img_roi = self.frame[yy0:yy0 + yy1, xx0:xx0 + xx1]
                cv2.imshow("Tracking Window", img_roi)
                if get_window_size(
                        self.track_window) >= size_treshold and Update:
                    self.bkp = self.hist
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    cv2.putText(vis, 'Updating...', (10, 200), font, 1,
                                (255, 255, 255), 2, 1)
                    xx0, yy0, xx1, yy1 = self.track_window
                    xx1 /= 3
                    yy1 /= 3
                    xx0 += xx1
                    yy0 += yy1
                    if xx1 > 0 and yy1 > 0:
                        # print self.track_window
                        hsv_roi = hsv[yy0:yy0 + yy1, xx0:xx0 + xx1]
                        mask_roi = mask[yy0:yy0 + yy1, xx0:xx0 + xx1]
                        cv2.imshow("Tracking Window", hsv_roi)
                        hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16],
                                            [0, 180])
                        cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                        # print cv2.compareHist(hist.reshape(-1), self.bkp, 0)
                        self.hist = hist.reshape(-1)
                    self.show_hist()
                    if not Realtime:
                        Update = not Update
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(vis, str(track_box[0]), (10, 400), font, 1,
                            (255, 255, 255), 2, 1)

                y = track_box[0]
                mark.draw_machine_mark(60, track_box[0], vis)

            if flag:
                height, width, _ = vis.shape
                if y:
                    for i in range(1, 5):
                        draw_rect(vis, depth=i, angle=(y[0] - width / 2) / 10)
                cv2.imshow('camshift', vis)

            ch = 0xFF & cv2.waitKey(50)
            if ch == 27:
                return
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
            if ch == ord('m'):
                MorphOps = not MorphOps
            if ch == ord('c'):
                Channel = not Channel
            if ch == ord('u'):
                Update = not Update
            if ch == ord('r'):
                Realtime = not Realtime
        return y
Ejemplo n.º 5
0
def camshift(cap, rec, initial_left, initial_top, initial_width,
             initial_height):

    ret, frame = cap.read()
    track_window = (initial_left, initial_top, initial_width, initial_height)

    # set up the ROI for tracking
    roi = frame[initial_top:initial_top + initial_height,
                initial_left:initial_left + initial_width]
    hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

    # 60, 32
    #255
    mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                       np.array((180., 255., 255.)))
    roi_hist = cv2.calcHist([hsv_roi], [0], mask, [20], [0, 180])
    cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

    # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
    term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

    # thresholds
    search_range_th = 15
    rec_size_th = 140
    dist_th = 30
    scale_th = 1.8

    target_center = (0, 0)
    past_target_center = (0, 0)

    color = (49, 78, 234)
    start_flag = False
    end_flag = False

    while (True):

        ret, frame = cap.read()

        if ret == True:
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

            # apply camshift to get the new location
            ret, track_window = cv2.CamShift(dst, track_window, term_crit)

            x, y, w, h = track_window

            target_center = (x + int(w / 2), y + int(h / 2))
            height, width = frame.shape[:2]

            # threshold process

            # size threshold
            if w > rec_size_th or h > rec_size_th:
                # print("over size")
                break

            # frame range threshold
            elif target_center[0] < search_range_th or target_center[1] < search_range_th or \
            target_center[0] > width-search_range_th or \
            target_center[1] > height-search_range_th:
                # print("over range")
                break

            # cener distance and rectangle scale threshold
            elif start_flag is True:
                dist = np.linalg.norm(
                    np.asarray(past_target_center) - np.asarray(target_center))
                w_scale = w / past_w
                h_scale = h / past_h

                # print(w_scale)
                # print(h_scale)
                # print()

                if dist > dist_th:
                    # print("over dist")
                    break
                elif w_scale > scale_th and h_scale > scale_th:
                    # print("over scale")
                    break

            # Draw it on image
            tracking_result = cv2.rectangle(frame, (x, y), (x + w, y + h),
                                            color, 2)
            cv2.circle(tracking_result, target_center, 2, (0, 215, 253), 2)
            cv2.putText(tracking_result, 'Tracking with Camshift ...',
                        (10, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
            cv2.putText(tracking_result, 'TARGET', (x, y + h + 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
            # pts = cv2.boxPoints(ret)
            # pts = np.int0(pts)
            # img2 = cv2.polylines(frame,[pts],True, (0,255,0),2)
            cv2.imshow('video', tracking_result)

            k = cv2.waitKey(60) & 0xff
            if k == 27:
                end_flag = True

            past_target_center = target_center
            past_w = w
            past_h = h
            start_flag = True

            if not rec == False:
                rec.write(tracking_result)
        else:
            break

    return end_flag
Ejemplo n.º 6
0
    def run(self):
        while True:

            self.frame = get_video()
            self.depth = get_depth()

            vis = self.frame.copy()
            vis1 = self.depth.copy()

            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1 - x0, y1 - y0)
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 1:
                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)

                xPos = track_box[0][0]
                yPos = track_box[0][1]
                #w = int(track_box[1][0])
                #h = int(track_box[1][1])

                xp = int(xPos)
                yp = int(yPos)

                kernel = np.ones((5, 5), np.uint8)
                vis1 = cv2.dilate(vis1, kernel, iterations=4)
                vis1 = cv2.erode(vis1, kernel, iterations=2)
                #vis1 = cv2.morphologyEx(vis1, cv2.MORPH_CLOSE, kernel)
                zPos = vis1[yp, xp]

                #data = str(xPos) +','+str(yPos)
                data = str(xPos) + ',' + str(yPos) + ',' + str(zPos)

                print "position - X:", str(xPos)
                print "position - Y:", str(yPos)
                print "position - Z:", str(zPos)
                #print "position - Z:", str(track_box)
                sock.sendto(str(data), (UDP_IP, UDP_PORT))

                if self.show_backproj:
                    vis[:] = prob[..., np.newaxis]
                try:
                    cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                except:
                    print track_box

            cv2.imshow('camshift', vis)
            cv2.imshow('depth', vis1)

            ch = 0xFF & cv2.waitKey(5)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
Ejemplo n.º 7
0
def main(args):
    #Clean previous image
    clean_images()
    #Training phase
    model = training()

    vidcap = cv2.VideoCapture(args.file_name)

    fps = vidcap.get(cv2.CAP_PROP_FPS)
    width = vidcap.get(3)  # float
    height = vidcap.get(4)  # float

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi', fourcc, fps, (640, 480))

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by a least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None
    roiHist = None

    success = True
    similitary_contour_with_circle = 0.65  # parameter
    count = 0
    current_sign = None
    current_text = ""
    current_size = 0
    sign_count = 0
    coordinates = []
    position = []
    file = open("Output.txt", "w")
    while True:
        success, frame = vidcap.read()
        if not success:
            print("FINISHED")
            break
        width = frame.shape[1]
        height = frame.shape[0]
        #frame = cv2.resize(frame, (640,int(height/(width/640))))
        frame = cv2.resize(frame, (640, 480))

        print("Frame:{}".format(count))
        #image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        coordinate, image, sign_type, text = localization(
            frame, args.min_size_components,
            args.similitary_contour_with_circle, model, count, current_sign)
        if coordinate is not None:
            cv2.rectangle(image, coordinate[0], coordinate[1], (255, 255, 255),
                          1)
        print("Sign:{}".format(sign_type))
        if sign_type > 0 and (not current_sign or sign_type != current_sign):
            current_sign = sign_type
            current_text = text
            top = int(coordinate[0][1] * 1.05)
            left = int(coordinate[0][0] * 1.05)
            bottom = int(coordinate[1][1] * 0.95)
            right = int(coordinate[1][0] * 0.95)

            position = [
                count, sign_type if sign_type <= 8 else 8, coordinate[0][0],
                coordinate[0][1], coordinate[1][0], coordinate[1][1]
            ]
            cv2.rectangle(image, coordinate[0], coordinate[1], (0, 255, 0), 1)
            font = cv2.FONT_HERSHEY_PLAIN
            cv2.putText(image, text, (coordinate[0][0], coordinate[0][1] - 15),
                        font, 1, (0, 0, 255), 2, cv2.LINE_4)

            tl = [left, top]
            br = [right, bottom]
            print(tl, br)
            current_size = math.sqrt(
                math.pow((tl[0] - br[0]), 2) + math.pow((tl[1] - br[1]), 2))
            # grab the ROI for the bounding box and convert it
            # to the HSV color space
            roi = frame[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)

            # compute a HSV histogram for the ROI and store the
            # bounding box
            roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
            roiBox = (tl[0], tl[1], br[0], br[1])

        elif current_sign:
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

            # apply cam shift to the back projection, convert the
            # points to a bounding box, and then draw them
            (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
            pts = np.int0(cv2.boxPoints(r))
            s = pts.sum(axis=1)
            tl = pts[np.argmin(s)]
            br = pts[np.argmax(s)]
            size = math.sqrt(pow((tl[0] - br[0]), 2) + pow((tl[1] - br[1]), 2))
            print(size)

            if current_size < 1 or size < 1 or size / current_size > 30 or math.fabs(
                (tl[0] - br[0]) / (tl[1] - br[1])) > 2 or math.fabs(
                    (tl[0] - br[0]) / (tl[1] - br[1])) < 0.5:
                current_sign = None
                print("Stop tracking")
            else:
                current_size = size

            if sign_type > 0:
                top = int(coordinate[0][1])
                left = int(coordinate[0][0])
                bottom = int(coordinate[1][1])
                right = int(coordinate[1][0])

                position = [
                    count, sign_type if sign_type <= 8 else 8, left, top,
                    right, bottom
                ]
                cv2.rectangle(image, coordinate[0], coordinate[1], (0, 255, 0),
                              1)
                font = cv2.FONT_HERSHEY_PLAIN
                cv2.putText(image, text,
                            (coordinate[0][0], coordinate[0][1] - 15), font, 1,
                            (0, 0, 255), 2, cv2.LINE_4)
            elif current_sign:
                position = [
                    count, sign_type if sign_type <= 8 else 8, tl[0], tl[1],
                    br[0], br[1]
                ]
                cv2.rectangle(image, (tl[0], tl[1]), (br[0], br[1]),
                              (0, 255, 0), 1)
                font = cv2.FONT_HERSHEY_PLAIN
                cv2.putText(image, current_text, (tl[0], tl[1] - 15), font, 1,
                            (0, 0, 255), 2, cv2.LINE_4)

        if current_sign:
            sign_count += 1
            coordinates.append(position)

        cv2.imshow('Result', image)
        count = count + 1
        #Write to video
        out.write(image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    file.write("{}".format(sign_count))
    for pos in coordinates:
        file.write("\n{} {} {} {} {} {}".format(pos[0], pos[1], pos[2], pos[3],
                                                pos[4], pos[5]))
    print("Finish {} frames".format(count))
    file.close()
    return
Ejemplo n.º 8
0
    def run(self):
        while True:
            _ret, self.frame = self.cam.read()
            vis = self.frame.copy()
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.selection
                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.track_window and self.track_window[
                    2] > 0 and self.track_window[3] > 0:
                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)

                if self.show_backproj:
                    vis[:] = prob[..., np.newaxis]
                try:
                    # Magica
                    cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                    #Envio e impressão dos dados do rastreamento

                    X = Alema1map(
                        track_box[0][0], 0, 640, 0,
                        255)  #X convertido pra int variando de 0 a 640px
                    Y = Alema1map(
                        track_box[0][1], 0, 480, 0,
                        255)  #Y convertido pra int variando de 0 a 480px
                    comport.write([3, X])  #envia no serial a coordenada X
                    comport.write([4, Y])  #envia no serial a coordenada Y
                    print(X)
                    print(Y)
                    time.sleep(0.05)
                    #comport.write(track_box [0][1])#coordenada Y
                    #print("Coordenada X")
                    #print(track_box [0][0])#coordenada X
                    #a= comport.Read()
                    #print('batatinha')
                    #print("Coordenada Y")
                    #print(track_box [0][1])#coordenada Y

                except:
                    print('ih rapaz')

            cv2.imshow('SPEAR Eye', vis)

            ch = cv2.waitKey(5)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
        cv2.destroyAllWindows()
def cmShift_tracker(v, file_name):
    # Open output file
    output_name = sys.argv[3] + file_name
    output = open(output_name,"w")

    frameCounter = 0
    # read first frame
    ret ,frame = v.read()

    #debug
    # cv2.imshow('frame',frame)
    # cv2.waitKey(0)
    # print(type(frame),frame)

    if ret == False:
        return

    # detect face in first frame
    c,r,w,h = detect_one_face(frame)
    # Write track point for first frame

    pt_x, pt_y=c + w/2.0,r + h/2.0
    #channged
    output.write("%d,%d,%d\n" % (frameCounter,pt_x,pt_y)) # Write as 0,pt_x,pt_y
    frameCounter = frameCounter + 1

    # set the initial tracking window
    track_window = (c,r,w,h)

    # calculate the HSV histogram in the window
    # NOTE: you do not need this in the Kalman, Particle or OF trackers
    roi_hist = hsv_histogram_for_window(frame, (c,r,w,h)) # this is provided for you

    # initialize the tracker
    # e.g. kf = cv2.KalmanFilter(4,2,0)
    # or: particles = np.ones((n_particles, 2), int) * initial_pos

    while(1):
        ret ,frame = v.read() # read another frame
        if ret == False:
            break

        # perform the tracking
        # e.g. cv2.meanShift, cv2.CamShift, or kalman.predict(), kalman.correct()

        # use the tracking result to get the tracking point (pt):
        # if you track a rect (e.g. face detector) take the mid point,
        # if you track particles - take the weighted average
        # the Kalman filter already has the tracking point in the state vector

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        prob = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        retval,track_window=cv2.CamShift(prob,track_window,term_crit)

        # Draw it on image
        # print(retval)
        pts = cv2.boxPoints(retval)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame,[pts],True, 255,2)
        cv2.imshow('img2',img2)
        k = cv2.waitKey(60) & 0xff
        c,r,w,h=track_window

        # write the result to the output file
        output.write("%d,%d,%d\n" % (frameCounter,c + w/2.0,r + h/2.0)) # Write as frame_index,pt_x,pt_y
        frameCounter = frameCounter + 1

    output.close()
Ejemplo n.º 10
0
        # 1) separamos los canales y calculamos la posición de sus
        #    valores en el histograma
        b, g, r = (frame // (256 // bins)).transpose(2, 0, 1)

        # 2) obtenemos el valor del histograma en toda la imagen
        #    aprovechando características de numpy
        L = H[b, g, r]

        # las imágenes de float se muestran en la escala 0:negro 1:blanco
        cv.imshow('likelihood', L / L.max())

        if True:
            # tracking mediante camshift de opencv
            term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
            elipse, track_window = cv.CamShift(L, track_window, term_crit)
            cv.ellipse(frame, elipse, (0, 128, 255), 2)
            (c, r, w, h) = track_window
            cv.rectangle(frame, (c, r), (c + w, r + h), (0, 128, 255), 1)

        # implementación propia de mean shift

        # mostramos la posición anterior
        cv.rectangle(frame, (cm - szx, rm - szy), (cm + szx, rm + szy),
                     (255, 128, 255), 2)

        # preparamos una máscara de tamaño doble para calcular la media
        # en un entorno del objeto
        mask = np.zeros_like(L, np.uint8)
        s = 2
        # thickness = -1 rellena el rectángulo
Ejemplo n.º 11
0
def find_object_by_color(color=172, pctErr=.1):
    aoLocList = [(0, 0)]  #average object location list

    #for each image
    for file_num in [str(x) for x in range(0, 7)]:

        #cap = cv.VideoCapture(0)
        #while(1):
        #_wtf, frame = cap.read()
        print "File:" + file_num + ".jpg"
        frame = cv.imread(file_num + '.jpg', cv.IMREAD_COLOR)

        print "frame.shape=", frame.shape
        frame_y, frame_x = frame.shape[:2]

        #set the default first average object location to the middle of the screen
        aoLocList[0] = (frame_x / 2, frame_y / 2)

        hsv = cv.cvtColor(
            frame,
            cv.COLOR_BGR2HSV)  #Convert the captured frame from BGR to HSV

        # Threshold the HSV image to get only red colors
        #lower_red = np.array([165,100,100]) #RGB
        #upper_red = np.array([180,255,255])
        # Threshold the HSV image to be within 10% of the requested
        lower_red = np.array([color * (1 - pctErr), 100, 100])  #RGB
        upper_red = np.array([color * (1 + pctErr), 255, 255])
        mask = cv.inRange(hsv, lower_red, upper_red)

        kernel = np.ones((5, 5), np.uint8)
        eroded_mask = cv.dilate(mask, kernel, iterations=3)
        #DEBUG#cv.imshow('eroded_mask', eroded_mask)

        # Set tracking to start with the full frame
        track_window = (0, 0, frame_x, frame_y)
        print track_window

        # Bitwise-AND mask and original image
        #res = cv.bitwise_and(img,img, mask= mask)
        #cv.imshow('res',res)

        # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
        term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)

        sys.stdout.flush()
        #Run camshift x times and draw the box
        # apply meanshift to get the new location
        # camshift returns tw[0]=y, tw[1]=x, tw[2]=h, tw[3]=w
        ret, track_window = cv.CamShift(mask, track_window, term_crit)
        tw = track_window

        # for averages we want x1+x2/2, = tw[1]+tw[1]+tw[3]/2 = 2*tw[1]+tw[3]/2 = tw1+tw3,
        #print "track_window (x,w,y,h) = ", track_window #DEBUG#

        # Draw it on image
        pts = cv.boxPoints(ret)
        pts = np.int0(pts)
        img = cv.polylines(frame, [pts], True, 255, 2)
        #cv.imshow('track', track_window)

        #calculate vector for each frame after camshift
        #aoLocList.append ((tw[1]+tw[3]/2, tw[0]+tw[2]/2))
        aoLocList.append((tw[0] + tw[2] / 2, tw[1] + tw[3] / 2))

        #object_vector = (aoLocList[-2][0] - aoLocList[-1][0], aoLocList[-2][1] - aoLocList[-1][1])
        object_loc = aoLocList[-1]
        prev_object_loc = aoLocList[-2]
        object_vector = [
            object_loc[i] - prev_object_loc[i] for i in range(len(object_loc))
        ]

        print "aoLocList", aoLocList
        print "object_vector", object_vector
        print frame_x, frame_y
        #draw_arrow not working vector line
        #draw arrow from middle representing magnitude and direction
        f.draw_arrow(
            img, (frame_x / 2, frame_y / 2),
            (object_vector[0] - frame_x / 2, object_vector[1] - frame_y / 2))
        #draw line from previous center to current center
        cv.line(img, (prev_object_loc), (object_loc), (200, 10, 40), 3)
        print "draw line at:", (frame_x / 2, frame_y / 2), object_vector
        #img = cv.line(img, (x/2, y/2), object_vector, (60,20,200))

        #cv.imshow('frame', frame[tw[1]:tw[1]+tw[3], tw[0]:tw[0]+tw[2]])
        cv.imwrite(
            os.path.join('image_results', "t_camShift" + file_num + ".jpg"),
            img)
        cv.imwrite(os.path.join('image_results', "t_mask" + file_num + ".jpg"),
                   eroded_mask)

        #I would love to learn more about this method and what I'm doing wrong
        #roi_hist = cv.calcHist([hsv],[0],eroded_mask,[180],[10,160])
        #cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)
        #dst = cv.calcBackProject([hsv], [0], roi_hist, [20,180], 1)
        #cv.imwrite(os.path.join('image_results',"t_BackProp" + file_num + ".jpg"), dst)

        print "image saved test_" + file_num + ".jpg"

        sys.stdout.flush()
        #pause
        #k = cv.waitKey()
        #if k == 27:   # wait for ESC key to exit any other key continues operation
        #    break
    cv.destroyAllWindows()
Ejemplo n.º 12
0
    def run(self):
        roi = self.roi
        self.start()
        while True:
            #         for frame in self.cam.capture_continuous(self.rCa, format='bgr', use_video_port=True):
            #读取视频帧
            ret, self.frame = self.cam.read()
            #             self.frame = frame.array
            vis = self.frame.copy()
            #             vis = copy.deepcopy(self.frame)
            #把图像从rgb转换为hsv颜色空间,色调 饱和度 亮度
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            #函数去除阈值,去除背景部分
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((255., 255., 255.)))
            #             self.selection = 1

            if self.selection:
                #                 x0, y0, x1, y1 = 220, 110, 358, 245
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1 - x0, y1 - y0)
                #                 hsv_roi = hsv[y0:y1, x0:x1]
                #                 mask_roi = mask[y0:y1, x0:x1]
                hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
                mask_roi = cv2.inRange(hsv_roi, np.array((0., 0., 0.)),
                                       np.array((255., 255., 255.)))
                #一维直方图
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [32], [0, 180])
                #二维直方图
                #hist = cv2.calcHist( [hsv_roi], [0,2],None, [180,256], [0, 180,0 , 255] )
                #hist = cv2.calcHist( [hsv_roi], [0, 2],None, [180,256], [0, 180, 0, 255] )
                #将绘制出来的直方图归一化,设定显示范围
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                #二维直方图显示
                #               plt.imshow(hist,interpolation = 'nearest')
                #               plt.show()
                #		plt.imshow(hist,interpolation = 'nearest')
                #               plt.show()
                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                #bitwise_not是对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作,~1=0,~0=1
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 1:
                self.selection = None
                #反向投影
                #BackProjection中存储的数值代表了测试图像中该像素属于红色、蓝色区域的概率
                #这里是图像函数,channel,掩模图像,像素值范围,(可选输出反向投影的比例因子) 像素是否均匀分布
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                #设置迭代的终止标准,最多十次迭代
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)
                #                 if track_box[0][1] <= 240:
                #             self.ser.write(str(int(track_box[0][0])-320) + " " + str(int(track_box[0][1])-240))
                #             print str(int(track_box[0][0])-320) + " " + str(int(track_box[0][1])-240)
                if track_box[1][1] <= 1:
                    self.tracking_state = 0
                    self.start()
                    print(track_box)
                    print(
                        "===================================================")
                else:
                    if self.show_backproj:
                        vis[:] = prob[..., np.newaxis]
                    try:
                        cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                        #                         print track_box
                        # 中心的x,y坐标;宽,高;角度
                        a = str(track_box[0][0])+" "+str(track_box[0][1])+" "+str(round(track_box[1][0],2))\
                                       +" "+str(round(track_box[1][1],2))+" "+str(round(track_box[2],2))+"\r\n"
                        print(a)


#                         self.ser.write(a)
                    except:
                        print(track_box)

            cv2.imshow('camshift', vis)

            ch = 0xFF & cv2.waitKey(5)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
            if ch == ord('r'):
                self.tracking_state = 0
                self.start()
        cv2.destroyAllWindows()
Ejemplo n.º 13
0
    def update(self):
        flag = False
        track_window = (250, 90, 400, 125)
        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        cascade = cv2.CascadeClassifier('rpalm.xml')

        #setting of letters display

        width = cv2.VideoCapture.get(self.capture, cv2.CAP_PROP_FRAME_WIDTH)
        height = cv2.VideoCapture.get(self.capture, cv2.CAP_PROP_FRAME_HEIGHT)

        font = cv2.FONT_HERSHEY_SIMPLEX
        fontScale = 0.9
        fontColor = (255, 255, 255)
        lineType = 2
        bottomLeftCornerOfText = (0, int(height * 0.95))

        while True:
            if self.capture.isOpened():
                # Read frame
                (self.status, self.frame) = self.capture.read()
                self.frame = cv2.flip(self.frame, 1)
                key = cv2.waitKey(2)

                # Close program with keyboard 'q'
                if key == ord('q'):
                    cv2.destroyAllWindows()
                    exit()

                # Crop and display cropped image
                if flag:
                    hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
                    dst = cv2.calcBackProject([hsv], [0], self.roi_hist,
                                              [0, 180], 1)
                    edges = cv2.Canny(dst, 0, 100)

                    #cv2.imshow('canny edges',edges)
                    rect_kernel = cv2.getStructuringElement(
                        cv2.MORPH_ELLIPSE, (20, 20))
                    edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE,
                                             rect_kernel)
                    #cv2.imshow('img after closing the edges',edges)

                    cv2.imshow('img dst', dst)
                    ret, thresh = cv2.threshold(dst, 100, 255, 0)
                    contours, hierarchy = cv2.findContours(
                        edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
                    contour_sizes = [(cv2.contourArea(contour), contour)
                                     for contour in contours]

                    contours_poly = [None] * len(contours)
                    boundRect = [None] * len(contours)
                    centers = [None] * len(contours)
                    radius = [None] * len(contours)
                    for i, c in enumerate(contours):
                        contours_poly[i] = cv2.approxPolyDP(c, 3, True)
                        boundRect[i] = cv2.boundingRect(contours_poly[i])
                    drawing = np.zeros(thresh.shape, np.uint8)

                    for i in range(len(contours)):
                        color = (rng.randint(0, 256), rng.randint(0, 256),
                                 rng.randint(0, 256))
                        cv2.drawContours(drawing, contours_poly, i, color)

                    #cv2.imshow('Contours', drawing)

                    if len(contour_sizes) > 0:
                        mask = np.zeros(thresh.shape, np.uint8)
                        biggest_contour = max(contour_sizes,
                                              key=lambda x: x[0])[1]
                        bRect = cv2.boundingRect(
                            cv2.approxPolyDP(biggest_contour, 3, True))
                        cv2.drawContours(drawing, contours_poly, i, color)
                        cv2.rectangle(self.frame,
                                      (int(bRect[0]), int(bRect[1])),
                                      (int(bRect[0] + bRect[2]),
                                       int(bRect[1] + bRect[3])), color, 2)

                        cropp_img = self.frame[int(bRect[1]):int(bRect[1] +
                                                                 bRect[3]),
                                               int(bRect[0]):int(bRect[0] +
                                                                 bRect[2])]
                        cv2.imshow("cropped", cropp_img)

                        cv2.drawContours(mask, contours, -1, 255, -1)
                        #cv2.imshow('mask', mask)

                        ret, track_window = cv2.CamShift(
                            mask, track_window, term_crit)
                        pts = cv2.boxPoints(ret)
                        pts = np.int0(pts)

                        if self.translator != None:
                            # TRANSLATION OF CROPED IMAGE
                            l1, p1, l2, p2, l3, p3 = self.translator.translate(
                                cropp_img)
                            s = str(l1) + ': ' + str(round(
                                p1, 3)) + str('    ') + str(l2) + ': ' + str(
                                    round(p2, 3)) + str('    ') + str(
                                        l3) + ': ' + str(round(p3, 3))
                            cv2.putText(self.frame, s, bottomLeftCornerOfText,
                                        font, fontScale, fontColor, lineType)

                else:
                    while flag == False:
                        (self.status, self.frame) = self.capture.read()
                        key = cv2.waitKey(2)
                        clone = self.frame.copy()
                        flag = self.crop_ROI(cascade)
                        if flag:
                            self.setup_ROI_tracking()
                        cv2.imshow('image', self.frame)

                cv2.imshow('image', self.frame)

            else:
                pass
Ejemplo n.º 14
0
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
print(roi_hist.shape)
# setup the termination criteria, either 10 iteration or move by at least 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
cv2.imshow('roi', roi)

while 1:
    ret, frame = cap.read()
    if ret:

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)  # ret has value x, y, w, h, rot
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        final_img = cv2.polylines(frame, [pts], True, (255, 255, 9), 2)
        cv2.imshow('dst', dst)
        cv2.imshow('final', final_img)
        cv2.imshow('frame', frame)
        k = cv2.waitKey(1)
        if k == 27:
            break

    else:
        break

cap.release()
cv2.destroyAllWindows()
Ejemplo n.º 15
0
def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="path to the (optional) video file")
    args = vars(ap.parse_args())

    global frame, roiPts, inputMode, roiBoxWidth, roiBoxHeight
    centerX = 0
    centerY = 0

    if not args.get("video", False):
        camera = cv2.VideoCapture(0)

    else:
        camera = cv2.VideoCapture(args["video"])

    cv2.namedWindow("frame")
    cv2.setMouseCallback("frame", selectROI)
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None

    while True:
        (grabbed, frame) = camera.read()

        if not grabbed:
            break

        if roiBox is not None:
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)
            if roiBoxWidth > 0 and roiBoxHeight > 0:
                (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
                roiBoxWidth = roiBox[2]
                roiBoxHeight = roiBox[3]

            else:

                print "roiBox init !!!!!!!!!!!!!"
                tl[0] = 1
                tl[1] = 1
                br[0] = Width - 1
                br[1] = Height - 1
                roiBox = (tl[0], tl[1], br[0], br[1])
                (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
                roiBoxWidth = roiBox[2]
                roiBoxHeight = roiBox[3]

            pts = np.int0(cv2.cv.BoxPoints(r))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 2)
            centerX = (pts[0][0] + pts[2][0]) / 2
            centerY = (pts[0][1] + pts[2][1]) / 2

        cv2.imshow("frame", frame)
        key = cv2.waitKey(1) & 0xFF

        if key == ord("i") and len(roiPts) < 4:
            inputMode = True
            orig = frame.copy()

            while len(roiPts) < 4:
                cv2.imshow("frame", frame)
                cv2.waitKey(0)

            roiPts = np.array(roiPts)
            s = roiPts.sum(axis=1)
            tl = roiPts[np.argmin(s)]
            br = roiPts[np.argmax(s)]

            roi = orig[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
            roiBox = (tl[0], tl[1], br[0], br[1])

            roiBoxWidth = roiBox[2]
            roiBoxHeight = roiBox[3]

        elif key == ord("q"):
            break
    camera.release()
    cv2.destroyAllWindows()
def main():

    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="Input a video")
    args = vars(ap.parse_args())

    # grab the reference to the current frame, list of ROI
    # points and whether or not it is ROI selection mode
    global frame, roiPts, inputMode
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    # if the video path was not supplied, grab the reference to the
    # camera
    if not args.get("video", False):
        camera = cv2.VideoCapture(0)

    # otherwise, load the video
    else:
        camera = cv2.VideoCapture(args["video"])

    # setup the mouse callback
    cv2.namedWindow("frame")
    # cv2.setMouseCallback("frame", selectROI)

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by a least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

    # initialize Variables
    frame, nextgray, prevgray, currROI1, currROI2, currROI3, normalizedrectangle1, normalizedrectangle2, normalizedrectangle3, nextframe, previousFrame, threshold, listtocheck, roiBox, tao = None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
    meanlist, listoftimes, roiPts, HistoryList, prevlistforehead, prevlistfleftface, prevlistrightface, prevallpoints = [], [], [], [], [], [], [], []
    a11, a22, c11, b11, b22, c22, prevframecounter, listcounter, firstframe, initframecounter, firsttimecount, cutlow, frameno, fcount = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    inputMode, resetloop, writetext, HRready = False, False, True, False
    enterif, notmoving, first, thritysecs = True, True, True, True
    detectioncount1, detectioncount2 = 0, 0
    countdowntime = 1
    result = []
    topleft, bottomleft, topright, bottomright = (0, 0), (0, 0), (0, 0), (0, 0)
    cuthigh = 10
    currentcount = 5
    ptsss = [(0, 0) for i in range(15)]
    ctd = "Calibrating"

    # reading the next frame frame and starting time
    nextframe = camera.read()
    start_time = time.time()
    # creating a file for writing
    textFilee = open(
        'test.txt', 'w'
    )  #this textfile records all heart rate values until the end of video or 'q' is pressed
    # saving the frames as .mp4 video
    # fourcc = cv2.VideoWriter_fourcc(*'MP4V')
    # out = cv2.VideoWriter('original_vid.mp4', fourcc, 25.0, (480, 640))
    # keep looping over the frames
    while True:
        # setting face not detected to begin with
        foundlandmark = False

        # resetting all the variable to initialed value when resetloop is envoked
        if resetloop == True:
            listtocheck = None
            firstframe = 1
            initframecounter = 0
            firsttimecount = 0
            countdowntime = 1
            resetloop = False
            enterif = True
            notmoving = True
            first = True

        # grab the current frame
        (grabbed, frame) = camera.read()
        # resizing the frame
        frame = cv2.resize(frame, (640, 480))
        # copying the frame
        HSVframe = copy.copy(frame)

        # setting precious frame and next frame
        prevframe = copy.copy(nextframe)
        nextframe = copy.copy(frame)
        # check to see if we have reached the end of the
        # video
        if not grabbed:
            break

        # if the see if the ROI has been computed
        if roiBox is not None:
            # convert the current frame to the HSV color space
            # and perform mean shift
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            backProj = cv2.calcBackProject([hsv], [0, 1], roiHist,
                                           [0, 180, 0, 256], 1)

            # Now convolute with circular disc
            disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
            cv2.filter2D(backProj, -1, disc, backProj)

            # threshold and binary AND
            ret, thresh = cv2.threshold(backProj, 50, 255, 0)
            thresh = cv2.merge((thresh, thresh, thresh))
            skinMask = cv2.erode(thresh, disc, iterations=2)
            skinMask = cv2.dilate(thresh, disc, iterations=2)

            # blur the mask to help remove noise, then apply the
            # mask to the frame
            skinMask = cv2.GaussianBlur(thresh, (3, 3), 0)
            res = cv2.bitwise_and(frame, skinMask)
            fcount = fcount + 1
            # draw a box on the indentified density

            (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
            pts = np.int0(cv2.boxPoints(r))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 2)

            # converting frame to grayscale
            gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

            rects = detector(gray, 0)
            # declaring variables
            ixc, iyc, a1x, a1y, a2x, a2y, a3x, a3y, a4x, a4y, a5lx, a5y, b1x, b1y, b2x, b2y, b3x, b3y, b4x, b4y, b5y, b5x, c1x, c1y, c2x, c2y, c3x, c3y, c4x, c4y, c5x, c5y, d1x, d1y, e1x, e1y = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

            for rect in rects:
                # determine the facial landmarks for the face region, then
                # convert the facial landmark (x, y)-coordinates to a NumPy
                # array
                shape = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)
                counter = 0

                # loop over the (x, y)-coordinates for the facial landmarks
                # and draw them on the image

                for (x, y) in shape:

                    cv2.circle(frame, (x, y), 4, (0, 0, 255), -1)
                    cv2.putText(frame, str(counter), (x, y),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255),
                                1)
                    # setting face as found
                    foundlandmark = True

                    # saving particular face landmarks for the ROI box
                    if counter == 21:
                        a1x = x
                        a1y = y / 1.3
                    if counter == 22:
                        a2x = x
                        a2y = y
                    if counter == 27:
                        a3x = x
                        a3y = y
                    if counter == 8:
                        a4x = x
                        a4y = y
                    if counter == 23:
                        a5x = x
                        a5y = y

                    if counter == 17:
                        b1x = x
                        b1y = y * 1.2
                    if counter == 31:
                        b2x = x
                        b2y = y
                    if counter == 28:
                        b3x = x
                        b3y = y
                    if counter == 39:
                        b4x = x
                        b4y = y
                        ixc = (a1x + a2x) / 2.2
                        iyc = (a4y + a3y)

                    if counter == 26:
                        c1x = x
                        c1y = y / 1.2
                    if counter == 35:
                        c2x = x
                        c2y = y
                    if counter == 28:
                        c3x = x
                        c3y = y
                    if counter == 42:
                        c4x = x
                        c4y = y

                    if counter == 16:
                        d1x = x * 1.1
                        d1y = y

                    if counter == 0:
                        e1x = x / 1.15
                        e1y = y

                    counter = counter + 1

                firstframe = 0
                initframecounter = initframecounter + 1

            # co-ordinates for the rectangle
            listforehead = [int(a1x), int(a1y), a2x, a2y]
            listleftface = [int(b1x), int(b3y), b4x, b2y]
            listrightface = [int(c1x), int(c3y), c4x, c2y]

            cv2.rectangle(frame, (listforehead[0], listforehead[1]),
                          (listforehead[2], listforehead[3]), (255, 0, 0), 2)
            cv2.rectangle(frame, (listleftface[0], listleftface[1]),
                          (listleftface[2], listleftface[3]), (255, 0, 0), 2)
            cv2.rectangle(frame, (listrightface[0], listrightface[1]),
                          (listrightface[2], listrightface[3]), (255, 0, 0), 2)

            # converting the frame to HSV
            HSVframe = CoverttoHSV(HSVframe)

            # checkig if this is the first frame
            if firstframe == 0:
                if first == True:
                    listtocheck = listforehead
                    firstframe = 1
                    first = False

            # setting up intital frames to measure the Bluriness value
            # setting up the blurriness threshold from the frist 6 seconds
            # checking the following frames and comparing it to the bluriness value
            # sharpen the frames if needed depending on the blurriness mean
            if (initframecounter / 2) < 6:
                if enterif == True:
                    if countdowntime == 0:
                        HistoryList.append(gray)
                    notmoving = checkpixeldiff(listtocheck, listforehead)
                    if notmoving == False:
                        text = "You Moved. Starting countdown again"
                        resetloop = True
                        HistoryList = []
                        continue

            else:
                if enterif == True:
                    initframecounter = 0
                    if countdowntime == 0:
                        notmoving = checkpixeldiff(listtocheck, listforehead)
                        if notmoving == True:
                            enterif = False
                            continue
                        else:
                            resetloop = True
                            HistoryList = []

                            continue
                    countdowntime = countdowntime - 1

            if enterif == False:

                threshold = findvarmean(HistoryList)
                if cv2.Laplacian(gray, cv2.CV_64F).var() < threshold:
                    HSVframe = cv2.bilateralFilter(HSVframe, 9, 75, 75)
                    gaussian = cv2.GaussianBlur(HSVframe, (9, 9), 10.0)
                    HSVframe = cv2.addWeighted(HSVframe, 1.5, gaussian, -0.5,
                                               0, HSVframe)

                else:
                    HistoryList.pop(listcounter)
                    HistoryList.append(gray)
                    threshold = findvarmean(HistoryList)
                    listcounter = listcounter + 1
                    if listcounter == len(HistoryList):
                        listcounter = 0

            if enterif == True:
                currentcount = countdowntime
                if foundlandmark == False:
                    ctd = "Searching For Face"
                else:
                    ctd = "Calibrating"

                ctd2 = ctd
                cv2.putText(frame, ctd, (30, 30), cv2.FONT_HERSHEY_PLAIN, 1.5,
                            (0, 0, 255))

            # setting the previous ROI to cerrent ROI
            prevROI1 = currROI1
            prevROI2 = currROI2
            prevROI3 = currROI3

            # setting the current ROI to next ROI
            currROI1 = gray[listforehead[1]:listforehead[1] + 10,
                            listforehead[0]:listforehead[0] + 10]
            currROI2 = gray[listleftface[1]:listleftface[1] + 10,
                            listleftface[0]:listleftface[0] + 10]
            currROI3 = gray[listrightface[1]:listrightface[1] + 10,
                            listrightface[0]:listrightface[0] + 10]

            pointsListRoi1, pointsListRoi2, pointsListRoi3, avelist1, avearray2, Normalizedlist = [], [], [], [], [], [
                8]

            # finding the middle points of the region of interest for Kalman Filter Calculation
            for x1 in range(1):
                a1 = int((listforehead[0] + listforehead[2]) / 2) + x1
                b1 = int((listleftface[0] + listleftface[2]) / 2) + x1
                c1 = int((listrightface[0] + listrightface[2]) / 2) + x1
                for y1 in range(5):
                    a2 = int((listforehead[1] + listforehead[3]) / 2) + y1
                    b2 = int((listleftface[1] + listleftface[3]) / 2) + y1
                    c2 = int((listrightface[1] + listrightface[3]) / 2) + y1

                    tup1 = (a1, a2)
                    tup2 = (b1, b2)
                    tup3 = (c1, c2)

                    pointsListRoi1.append(tup1)
                    pointsListRoi2.append(tup2)
                    pointsListRoi3.append(tup3)
                    allPoints = pointsListRoi1 + pointsListRoi2 + pointsListRoi3
                d = 0

            # if face is found
            if foundlandmark == True:
                # seeting the previous to current
                prevlistforehead = listforehead
                prevlistleftface = listleftface
                prevlistrightface = listrightface
                prevallpoints = allPoints
                topright = (pts[0][0], pts[0][1])
                bottomright = (pts[1][0], pts[1][1])
                topleft = (pts[3][0], pts[3][1])
                bottomleft = (pts[2][0], pts[2][1])

                # Passing the points to the Kalman filter
                ptsss = kalman_filter(topleft, bottomleft, topright,
                                      bottomright, allPoints, foundlandmark)

                # finding the length of the ROI
                a11 = int(abs(listforehead[0] - listforehead[2]) / 4)
                b11 = int(abs(listleftface[0] - listleftface[2]) / 4)
                c11 = int(abs(listrightface[0] - listrightface[2]) / 4)

                a22 = int(abs(listforehead[1] - listforehead[3]) / 4)
                b22 = int(abs(listleftface[1] - listleftface[3]) / 4)
                c22 = int(abs(listrightface[1] - listrightface[3]) / 4)

                ptsss2 = [ptsss[0], ptsss[5], ptsss[10]]

                # Finding the HSV value of the points of the ROI and storing it
                for xaxis in range(ptsss[0][0] - a11, ptsss[0][0] + a11):
                    for yaxis in range(ptsss[0][1] - a22, ptsss[0][1] + a22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])
                cv2.circle(frame, (ptsss[0][0], ptsss[0][1]), 8, (0, 0, 255),
                           -1)

                for xaxis in range(ptsss[5][0] - b11, ptsss[5][0] + b11):
                    for yaxis in range(ptsss[5][1] - b22, ptsss[5][1] + b22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])
                cv2.circle(frame, (ptsss[5][0], ptsss[5][1]), 8, (0, 0, 255),
                           -1)

                for xaxis in range(ptsss[10][0] - c11, ptsss[10][0] + c11):
                    for yaxis in range(ptsss[5][1] - c22, ptsss[5][1] + c22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])
                cv2.circle(frame, (ptsss[10][0], ptsss[10][1]), 8, (0, 0, 255),
                           -1)

                avearray2 = np.asarray(Normalizedlist)
                # taking the mean of the ROi
                totalmean = int(np.mean(avearray2))

            else:

                # When face not found work with previous values
                # Passing the points to the Kalman filter
                ptsss = kalman_filter(topleft, bottomleft, topright,
                                      bottomright, ptsss, foundlandmark)
                ptsss2 = [ptsss[0], ptsss[5], ptsss[10]]

                # Finding the HSV value of the points of the ROI and storing it
                for xaxis in range(ptsss[0][0] - a11, ptsss[0][0] + a11):
                    for yaxis in range(ptsss[0][1] - a22, ptsss[0][1] + a22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])

                # cv2.circle(frame, (ptsss[0][0], ptsss[0][1]), 8, (0, 0, 255), -1)

                for xaxis in range(ptsss[5][0] - b11, ptsss[5][0] + b11):
                    for yaxis in range(ptsss[5][1] - b22, ptsss[5][1] + b22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])

                # cv2.circle(frame, (ptsss[5][0], ptsss[5][1]), 8, (0, 0, 255), -1)

                for xaxis in range(ptsss[10][0] - c11, ptsss[10][0] + c11):
                    for yaxis in range(ptsss[5][1] - c22, ptsss[5][1] + c22):
                        Normalizedlist.append(HSVframe[yaxis][xaxis][0])
                # cv2.circle(frame, (ptsss[10][0], ptsss[10][1]), 8, (0, 0, 255), -1)

                avearray2 = np.asarray(Normalizedlist)
                # Taking the mean of the ROI
                totalmean = int(np.mean(avearray2))

            # upldating frame number and storing the mean
            frameno = frameno + 1
            end_time = time.time()  #record end time of program
            meanlist.append(totalmean)
            listoftimes.append(frameno)

            # converting to numpy array
            alll = np.asarray(meanlist)
            nptime = np.asarray(listoftimes)
            loop = False

            if foundlandmark == False:
                detectioncount1 = detectioncount1 + 1
            else:
                detectioncount1 = 0

            if detectioncount1 == 20:
                loop = True
                detectioncount1 = 0

            if foundlandmark == False and loop == True:
                roiPts = []
                roiBox = None

            # checking if enough samples are collected
            # Following is all the signal processing steps
            if len(
                    alll
            ) >= 125 + cutlow:  # fps = 30 frames/second, 300frames = 10 second window (30frames *10seconds)
                HRready = True
                global hr
                FPS = 25.00
                WINDOW_TIME_SEC = 5
                WINDOW_SIZE = int(np.ceil(WINDOW_TIME_SEC * FPS))
                windowStart = len(alll) - WINDOW_SIZE
                window = alll[windowStart:windowStart + WINDOW_SIZE]
                window = np.asarray(window)
                # ica = FastICA(whiten=False)
                window = (window - np.mean(window, axis=0)) / np.std(
                    window, axis=0)  # signal normalization
                window = np.reshape(window, (125, 1))
                # S = ica.fit_transform(window)  # ICA Part
                fs = FPS
                lowcut = 0.75
                highcut = 2.5
                detrend = scipy.signal.detrend(window)
                y = butter_bandpass_filter(detrend,
                                           lowcut,
                                           highcut,
                                           fs,
                                           order=5)
                powerSpec = np.abs(np.fft.fft(y, axis=0))**2
                freqs = np.fft.fftfreq(window.shape[0], 1.0 / fs)
                MIN_HR_BPM = 45.0
                MAX_HR_BMP = 150.0
                MAX_HR_CHANGE = 2.0
                SEC_PER_MIN = 60
                maxPwrSrc = np.max(powerSpec, axis=1)
                validIdx = np.where((freqs >= MIN_HR_BPM / SEC_PER_MIN)
                                    & (freqs <= MAX_HR_BMP / SEC_PER_MIN))
                validPwr = maxPwrSrc[validIdx]
                validFreqs = freqs[validIdx]
                maxPwrIdx = np.argmax(validPwr)
                hr = validFreqs[maxPwrIdx]
                cutlow = cutlow + FPS
                out6 = hr * 60
                result.append(out6)
                ave = np.asarray(result)
                out6 = int(np.mean(ave))

                global previous
                previous = out6
                # lock2.acquire() # uncomment this if graph is required
                textFile = open(
                    'bpmTemp.txt', 'w'
                )  #this textfile updates the graph if graph option is uncommented
                textFile.write(str(out6))
                textFile.close()
                # lock2.release() # uncomment this if graph is required

                tao = str('%.2f' % (out6))

                ce = 'BPM: ' + tao
                if enterif == False:
                    cv2.putText(frame, ce, (30, 30), cv2.FONT_HERSHEY_PLAIN,
                                1.5, (0, 0, 255))
                if writetext == True:
                    textFilee.write(
                        tao + '\n'
                    )  #this textfile records all heart rate values until the end of video or 'q' is pressed

            else:
                if enterif == False:
                    if HRready == False:
                        if foundlandmark == False:
                            ctd = "Searching For Face"
                        else:
                            ctd = "Calibrating"
                        cv2.putText(frame, ctd, (30, 30),
                                    cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255))

                    else:
                        t = str('%.2f' % out6)
                        fe = 'BPM: ' + t
                        cv2.putText(frame, fe, (30, 30),
                                    cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 255))
        cv2.imshow("frame", frame)
        cv2.imwrite("frame.jpg", frame)
        # out.write(frame) #comment out if saving video output
        key = cv2.waitKey(1) & 0xFF

        # handle if the 'i' key is pressed, then go into ROI
        # selection mode
        if len(roiPts) < 4:
            #if len(roiPts) < 4:
            # indicate that we are in input mode and clone the
            # frame
            inputMode = True
            orig = frame.copy()

            # keep looping until 4 reference ROI points have
            # been selected; press any key to exit ROI selction
            # mode once 4 points have been selected
            while len(roiPts) < 4:
                foundface = False
                face_cascade = cv2.CascadeClassifier(
                    'haarcascade_frontalface_default.xml')
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                top1 = (0, 0)
                top2 = (0, 0)
                bottom1 = (0, 0)
                bottom2 = (0, 0)
                for (x, y, w, h) in faces:
                    foundface = True
                    cv2.rectangle(frame, (int(x + 0.2 * w), y),
                                  (int(x + 0.8 * w), int(y + 0.78 * h)),
                                  (255, 0, 0), 2)
                    top1 = (int(x + 0.2 * w), y)
                    top2 = (int(x + 0.8 * w), y)
                    bottom1 = (int(x + 0.2 * w), int(y + 0.78 * h))
                    bottom2 = (int(x + 0.8 * w), int(y + 0.78 * h))

                cv2.imshow("frame", frame)
                # cv2.waitKey(0)

                if foundface == True:
                    roiPts.append(top1)
                    roiPts.append(top2)
                    roiPts.append(bottom1)
                    roiPts.append(bottom2)

            # determine the top-left and bottom-right points
            roiPts = np.array(roiPts)
            s = roiPts.sum(axis=1)
            tl = roiPts[np.argmin(s)]
            br = roiPts[np.argmax(s)]

            # grab the ROI for the bounding box and convert it
            # to the HSV color space
            roi = orig[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            # roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)

            # compute a HSV histogram for the ROI and store the
            # bounding box
            roiHist = cv2.calcHist([roi], [0, 1], None, [180, 256],
                                   [0, 180, 0, 256])

            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
            roiBox = (tl[0], tl[1], br[0], br[1])

        # if the 'q' key is pressed, stop the loop
        elif key == ord("q"):
            break
        # Press T for text file writing
        elif key == ord("t"):
            writetext = True

    # textFile.close()
    textFilee.close()
    # cleanup the camera and close any open windows
    camera.release()
    # out.release() #comment out if saving video output
    cv2.destroyAllWindows()
Ejemplo n.º 17
0
def main():

    global frame_histogram
    global track_window
    global capture

    # Capture camera device 0 (webcam)
    capture = cv2.VideoCapture(0)
    # Capture first frame
    success, frame = capture.read()

    if (not success):
        print "[Tracking] Error in video first capture process."

    # Init server
    server_instance = server.server()

    # Init algo
    frame, term_criteria, track_window, frame_histogram = init(frame)

    while (True):

        # Link mouse event to handleUserInteraction()
        cv2.setMouseCallback('Make Baby Groot dance', handleUserInteraction)

        # Capture frame-by-frame
        success, frame = capture.read()

        if (not success):
            print "[Tracking] Error in video capture process."
            break

        # Transform image from BGR to HSV
        hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        back_projection = cv2.calcBackProject([hsv_frame], [0],
                                              frame_histogram, [0, 180], 1)

        # Apply meanshift to get the new location
        ret, track_window = cv2.CamShift(back_projection, track_window,
                                         term_criteria)
        x, y, width, height = track_window[0], track_window[1], track_window[
            2], track_window[3]

        if (width * height > 1e-14):

            frame = drawROI(frame, x, y, width, height)

            # Call to server
            ratio_x = float((x + width / 2)) / float(numpy.size(frame, 0))
            server_instance.sendPosition(str(ratio_x))

        else:

            #print "[Tracking] Nothing tracked. Reset track window."
            x, y, width, height = 250, 400, 125, 90
            track_window = (x, y, width, height)

        key = cv2.waitKey(1)  # Returns -1 if no key pressed
        if (key == ord('q')) or (key == 27):  # Break if "q" or "esc" pressed
            break

        # Display the resulting frame
        cv2.imshow('Make Baby Groot dance', frame)
        cv2.imshow('Tracking', back_projection)
        #cv2.imshow('HSV',hsv_frame)

        # When everything done, release the capture
    capture.release()
    cv2.destroyAllWindows()

    return
Ejemplo n.º 18
0
                    print( "암호가 맞습니다." )

                    cv2.waitKey( 0 )
                    quit()
                else:
                    cv2.imwrite( 'Result.jpg', frame )

                    print( '암호가 새로 저장되었습니다. 다시 암호를 풀어주세요' )
                    start = False  # 이 while 문 종료하고, 다시 C를 누를 때까지 체크
                    # 검사
                
                    # Q를 누를 때까지 암호 만들기
    while trackWindow is not None:
        _, frame = cam.read()

        HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject( [HSV], [0], roi_hist, [0, 180], 1 )

        termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        getData, trackWindow = cv2.CamShift( dst, trackWindow, termination )

        pts = cv2.boxPoints( getData )
        pts = np.int0( pts )
        Show_frame = cv2.polylines( frame, [pts], True, (0, 255, 0), 2 )
        cv2.imshow('Showing', Show_frame)
except:
    pass

cv2.destroyAllWindows()
quit()
    def testCamshiftSelectROI(self):
        # from ComputerVisionOnline
        # http://www.computervisiononline.com/blog/tutorial-using-camshift-track-objects-video

        #ap = argparse.ArgumentParser()
        #ap.add_argument("-v", "--video",
        #    help = "path to the (optional) video file")
        #args = vars(ap.parse_args())

        # grab the reference to the current frame, list of ROI
        # points and whether or not it is ROI selection mode
        #global frame, roiPts, inputMode

        # if the video path was not supplied, grab the reference to the
        # camera
        #if not args.get("video", False):
        camera = cv2.VideoCapture(0)

        # otherwise, load the video
        #else:
        #    camera = cv2.VideoCapture(args["video"])

        # setup the mouse callback
        cv2.namedWindow("frame")
        cv2.setMouseCallback("frame", self.utilSelectROI)

        # initialize the termination criteria for cam shift, indicating
        # a maximum of ten iterations or movement by a least one pixel
        # along with the bounding box of the ROI
        termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
        roiBox = None

        # keep looping over the frames
        while True:
            # grab the current frame
            (grabbed, self.frame) = camera.read()
            self.frame = self.utilResizeImgIsoScale(self.frame)

            # check to see if we have reached the end of the
            # video
            if not grabbed:
                break

            # if the see if the ROI has been computed
            if roiBox is not None:
                # convert the current frame to the HSV color space
                # and perform mean shift
                hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
                backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180],
                                               1)
                cv2.imshow("backProj", backProj)

                # apply cam shift to the back projection, convert the
                # points to a bounding box, and then draw them
                (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
                pts = np.int0(cv2.cv.BoxPoints(r))
                cv2.polylines(self.frame, [pts], True, (0, 255, 0), 2)

            # show the frame and record if the user presses a key
            cv2.imshow("frame", self.frame)
            key = cv2.waitKey(1) & 0xFF

            # handle if the 'i' key is pressed, then go into ROI
            # selection mode
            if key == ord("i") and len(self.roiPts) < 4:
                # indicate that we are in input mode and clone the
                # frame
                self.inputMode = True
                orig = self.frame.copy()

                # keep looping until 4 reference ROI points have
                # been selected; press any key to exit ROI selction
                # mode once 4 points have been selected
                while len(self.roiPts) < 4:
                    cv2.imshow("frame", self.frame)
                    cv2.waitKey(0)

                # determine the top-left and bottom-right points
                self.roiPts = np.array(self.roiPts)
                s = self.roiPts.sum(axis=1)
                tl = self.roiPts[np.argmin(s)]
                br = self.roiPts[np.argmax(s)]

                # grab the ROI for the bounding box and convert it
                # to the HSV color space
                roi = orig[tl[1]:br[1], tl[0]:br[0]]
                roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
                #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)

                # compute a HSV histogram for the ROI and store the
                # bounding box
                roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
                roiHist = cv2.normalize(roiHist, roiHist, 0, 255,
                                        cv2.NORM_MINMAX)
                roiBox = (tl[0], tl[1], br[0], br[1])

            # if the 'q' key is pressed, stop the loop
            elif key == ord("q"):
                break

        # cleanup the camera and close any open windows
        camera.release()
        cv2.destroyAllWindows()
Ejemplo n.º 20
0
def color_tracking(drone_vision:DroneVisionGUI, bebop:Bebop):

    def show_hist(hist):
        """Takes in the histogram, and displays it in the hist window."""
        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])
            cv2.rectangle(img, (i * bin_w + 2, 255), ((i + 1) * bin_w - 2, 255 - h),
                          (int(180.0 * i / bin_count), 255, 255),
                          -1)
        img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
        cv2.imshow('hist', img)

    showBackProj = False
    showHistMask = False

    frame = drone_vision.get_latest_valid_picture()

    if frame is not None:
        (hgt, wid, dep) = frame.shape
        cv2.namedWindow('camshift')
        cv2.namedWindow('hist')
        cv2.moveWindow('hist', 700, 100)  # Move to reduce overlap

        # Initialize the track window to be the whole frame
        track_window = (0, 0, wid, hgt)
        #
        # Initialize the histogram from the stored image
        # Here I am faking a stored image with just a couple of blue colors in an array
        # you would want to read the image in from the file instead
        histImage = np.array([[[110, 70, 50]],
                              [[111, 128, 128]],
                              [[115, 100, 100]],
                              [[117, 64, 50]],
                              [[117, 200, 200]],
                              [[118, 76, 100]],
                              [[120, 101, 210]],
                              [[121, 85, 70]],
                              [[125, 129, 199]],
                              [[128, 81, 78]],
                              [[130, 183, 111]]], np.uint8)
        histImage = cv2.imread('orange.jpg')
        histImage = cv2.cvtColor(histImage,cv2.COLOR_BGR2HSV)
        maskedHistIm = cv2.inRange(histImage, np.array((0., 60., 32.)), np.array((180., 255., 255.)))
        cv2.imshow("masked",maskedHistIm)
        cv2.imshow("histim",histImage)
        hist = cv2.calcHist([histImage], [0], maskedHistIm, [16], [0, 180])
        cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
        hist = hist.reshape(-1)
        show_hist(hist)

        # start processing frames
        while cv2.getWindowProperty('camshift', 0) >= 0:
            frame = drone_vision.get_latest_valid_picture()
            vis = frame.copy()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # convert to HSV
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))  # eliminate low and high saturation and value values


            # The next line shows which pixels are being used to make the histogram.
            # it sets to black all the ones that are masked away for being too over or under-saturated
            if showHistMask:
                vis[mask == 0] = 0

            prob = cv2.calcBackProject([hsv], [0], hist, [0, 180], 1)
            prob &= mask
            term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
            track_box, track_window = cv2.CamShift(prob, track_window, term_crit)
            print(track_box[1][0]*track_box[1][1])

            if showBackProj:
                vis[:] = prob[..., np.newaxis]
            try:
                cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                area = track_box[1][0]*track_box[1][1]
                if area > 7000:
                    print("GOING BACK")
                    bebop.fly_direct(roll=0,pitch=-20,yaw=0,vertical_movement=0,duration=0.5)
                    #bebop.smart_sleep(1)
                elif area < 4000:
                    print("GOING FORWARD")
                    bebop.fly_direct(roll=0,pitch=20,yaw=0,vertical_movement=0,duration=0.5)
                    #bebop.smart_sleep(1)
            except:
                pass
                # print("Track box:", track_box)

            cv2.imshow('camshift', vis)

            ch = chr(0xFF & cv2.waitKey(5))
            if ch == 'q':
                break
            elif ch == 'b':
                showBackProj = not showBackProj
            elif ch == 'v':
                showHistMask = not showHistMask

    bebop.safe_land(10)
    cv2.destroyAllWindows()
Ejemplo n.º 21
0
y = 252
width = 455 - x
height = 395 - y
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180])

cap = cv2.VideoCapture(0)

term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

while True:
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

    ret, track_window = cv2.CamShift(mask, (x, y, width, height),
                                     term_criteria)

    pts = cv2.boxPoints(ret)
    pts = np.int0(pts)
    cv2.polylines(frame, [pts], True, (255, 0, 0), 2)

    cv2.imshow("mask", mask)
    cv2.imshow("Frame", frame)

    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
Ejemplo n.º 22
0
    if not params.locked:
        # Calculate average color
        params.hsv = cv2.mean(reigon1.getROI(hsv))
        # params.hsv2 = cv2.mean(reigon2.getROI(hsv))
        # params.hsv3 = cv2.mean(reigon3.getROI(hsv))

    mask = getMask(hsv, params.hsv)
    # mask = mask + getMask(hsv,params.hsv2)
    # mask = mask + getMask(hsv,params.hsv3)

    disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))

    cv2.filter2D(mask, -1, disc, mask)

    # apply meanshift to get the new location
    ret, track_window = cv2.CamShift(mask, track_window, term_crit)

    reigon2 = ROI(track_window)

    cv2.imshow('mask1', cv2.flip(mask, 1))
    # cv2.imshow('mask2',mask2)
    # cv2.imshow('mask3',mask3)

    reigon1.drawBoundary(frame)

    if reigon2 is not None:
        reigon2.drawBoundary(frame)
    # reigon2.drawBoundary(frame)
    # reigon3.drawBoundary(frame)
    # cv2.imshow('original',frame)
Ejemplo n.º 23
0
def color_recognition():

    video1 = cv2.VideoCapture(0)
    video2 = cv2.VideoCapture(1)
    _, frame1 = video1.read()
    _, frame2 = video2.read()
    h, w, _ = frame1.shape
    print(h, w)
    x1 = w/2
    y1 = h/2

    x2 = w/2
    y2 = h/2

    roi1 = cv2.imread("frame2_tubleron.jpg", 1)
    roi2 = cv2.imread("frame2_tubleron.jpg", 1)

    width1 = 300
    height1 = 120

    width2 = 300
    height2 = 120

    hsv_roi1 = cv2.cvtColor(roi1, cv2.COLOR_BGR2HSV)
    roi_hist1 = cv2.calcHist([hsv_roi1], [0], None, [180], [0, 180])
    roi_hist1[0] = 0
    roi_hist1 = cv2.normalize(roi_hist1, roi_hist1, 0, 255, cv2.NORM_MINMAX)

    hsv_roi2 = cv2.cvtColor(roi2, cv2.COLOR_BGR2HSV)
    roi_hist2 = cv2.calcHist([hsv_roi2], [0], None, [180], [0, 180])
    roi_hist2[0] = 0
    roi_hist2 = cv2.normalize(roi_hist2, roi_hist2, 0, 255, cv2.NORM_MINMAX)

    term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

    medstack1 = []
    medstack2 = []
    counter1 = 1
    counter2 = 1
    counter3 = 1
    num = 10
    text_file_out = open("Output.txt", "w")
    text_file_out.close()
    text_file_out = open("location.txt", "a")

    text_file_location = open("location.txt", "w")
    text_file_location.close()
    text_file_location = open("location.txt", "a")

    x_axis = 0
    y_axis = 0
    z_axis = 0


    while True:
        _, frame1 = video1.read()
        _, frame2 = video2.read()

        hsv1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)
        mask1 = cv2.calcBackProject([hsv1], [0], roi_hist1, [0, 180], 1)
        # mask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=3)
        # mask = cv2.erode(mask, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=1)
        # smask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20)), iterations=2)

        satNmask1 = cv2.bitwise_and(hsv1[:, :, 1], mask1)
        satNmask1 = cv2.dilate(satNmask1, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=3)
        satNmask1 = cv2.erode(satNmask1, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=1)
        threshold_indices1 = satNmask1 < 40
        satNmask1[threshold_indices1] = 0
        # satNmask = cv2.dilate(satNmask, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)), iterations=2)


        hsv2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2HSV)
        mask2 = cv2.calcBackProject([hsv2], [0], roi_hist2, [0, 180], 1)
        # mask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=3)
        # mask = cv2.erode(mask, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=1)
        # smask = cv2.dilate(mask, cv2.getStructuringElement(cv2.MORPH_RECT, (20, 20)), iterations=2)

        satNmask2 = cv2.bitwise_and(hsv2[:, :, 1], mask2)
        satNmask2 = cv2.dilate(satNmask2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=3)
        satNmask2 = cv2.erode(satNmask2, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)), iterations=1)
        threshold_indices2 = satNmask2 < 85
        satNmask2[threshold_indices2] = 0
        # satNmask = cv2.dilate(satNmask, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)), iterations=2)

        if counter1 < num:
            counter1 += 1
            medstack1.append(satNmask1)
            median_1 = np.median(medstack1, axis=0)
        else:
            _ = medstack1.pop(0)
            medstack1.append(satNmask1)
            median_1 = np.median(medstack1, axis=0)

        if counter2 < num:
            counter2 += 1
            medstack2.append(satNmask2)
            median_2 = np.median(medstack2, axis=0)
        else:
            _ = medstack2.pop(0)
            medstack2.append(satNmask2)
            median_2 = np.median(medstack2, axis=0)

        ret1, track_window1 = cv2.CamShift(median_1, (x1, y1, width1, height1), term_criteria)
        x1, y1, w1, h1 = track_window1
        pts1 = cv2.boxPoints(ret1)  # add cv. after cv2
        pts1 = np.intp(pts1)
        cv2.polylines(frame1,[pts1],True, 255,2)
        cv2.circle(frame1, (x1 + int(round(0.5 * w1)), y1 + int(round(0.5 * h1))), 5, (0, 0, 255), -1)
        font = cv2.FONT_HERSHEY_SIMPLEX

        ret2, track_window2 = cv2.CamShift(median_2, (x2, y2, width2, height2), term_criteria)
        x2, y2, w2, h2 = track_window2
        pts2 = cv2.boxPoints(ret2)  # add cv. after cv2.
        pts2 = np.intp(pts2)
        cv2.polylines(frame2, [pts2], True, 255, 2)
        cv2.circle(frame2, (x2 + int(round(0.5 * w2)), y2 + int(round(0.5 * h2))), 5, (0, 0, 255), -1)

        distance = abs(x1 - x2)
        new_dist = 0
        if distance != 0:
            new_dist = 5000/distance
        else:
            new_dist = new_dist
        cv2.putText(frame1, str(distance), ((x1 + int(round(0.5 * w1))), y1 + 4 + int(round(0.5 * h1))), font, 1,
                    (0, 0, 255), 2)
        cv2.putText(frame2, str(distance), ((x2 + int(round(0.5 * w2))), y2 + 4 + int(round(0.5 * h2))), font, 1,
                    (0, 0, 255), 2)
        phi = (min(x1,x2) + 0.5*abs(x1-x2) - 320) * 0.109375
        theta = (min(y1,y2) + 0.5*abs(y1-y2) ) * 0.113 + 62.86

        x_axis_temp = new_dist * math.sin(math.radians(theta)) * math.cos(math.radians(phi))
        y_axis_temp = new_dist * math.sin(math.radians(theta)) * math.sin(math.radians(phi))
        z_axis_temp = new_dist * math.cos(math.radians(theta))
        if (x_axis+ y_axis+ z_axis == 0) or (abs(x_axis - x_axis_temp) <50 and abs(y_axis - y_axis_temp) <50 and abs(z_axis - z_axis_temp) <50):
            x_axis = x_axis_temp
            y_axis = y_axis_temp
            z_axis = z_axis_temp


        cv2.imshow('frame1', frame1)
        cv2.imshow('frame2', frame2)
        cv2.imshow('median_1', median_1)
        cv2.imshow('median_2', median_2)
        if counter3 == 1:
            #text_file_out.write("theta =%d, phi =%d, distance = %f   x =%f,y =%f,z =%f   \n" % (theta,phi, new_dist, x_axis, y_axis, z_axis))
            text_file_location.write("%f\t%f\t%f\n" % (x_axis, y_axis, z_axis))

            counter3 = 1
        else:
            counter3 += 1
        # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        #cv2.imshow("Mask1", mask1)
        #cv2.imshow("sat1",hsv1[:,:,1])

        #cv2.imshow("satNmask1", satNmask1)
        # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # cv2.imshow("Mask2", mask2)
        # cv2.imshow("sat2",hsv2[:,:,1])
        #cv2.imshow("satNmask2", satNmask2)
        # cv2.imshow("average satNmask2",satNmask2_temp)

        key = cv2.waitKey(1)
        if key == 27:
            break
    video1.release()
    video2.release()

    cv2.destroyAllWindows()
Ejemplo n.º 24
0
    def run(self):
        print "Step6: run"
        roi = self.roi
        self.start()
        imCount = 0
        while True:
            print "Step10: run while"

            #         for frame in self.cam.capture_continuous(self.rCa, format='bgr', use_video_port=True):
            ret, self.frame = self.cam.read()
            #             self.frame = frame.array
            vis = self.frame.copy()
            #             vis = copy.deepcopy(self.frame)
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))  #创建一个遮罩mask
            #             self.selection = 1

            if self.selection:
                print "Step10: run while if selection"
                #                 x0, y0, x1, y1 = 220, 110, 358, 245
                x0, y0, x1, y1 = self.selection
                self.track_window = (x0, y0, x1 - x0, y1 - y0)
                #                 hsv_roi = hsv[y0:y1, x0:x1]
                #                 mask_roi = mask[y0:y1, x0:x1]
                hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
                mask_roi = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
                                       np.array((180., 255., 255.)))
                #一维直方图
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
                #二维直方图
                #                 hist = cv2.calcHist( [hsv_roi], [0,2],None, [180,256], [0, 180,0 , 255] )

                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                #二维直方图显示
                #                 plt.imshow(hist,interpolation = 'nearest')
                #                 plt.show()
                #                self.show_hist()

                vis_roi = vis[y0:y1, x0:x1]
                cv2.bitwise_not(vis_roi, vis_roi)
                vis[mask == 0] = 0

            if self.tracking_state == 1:
                print "Step11: run while if tracking_state"

                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)
                #                 if track_box[0][1] <= 240:
                #             self.ser.write(str(int(track_box[0][0])-320) + " " + str(int(track_box[0][1])-240))
                #             print str(int(track_box[0][0])-320) + " " + str(int(track_box[0][1])-240)
                if track_box[1][1] <= 1:
                    print "Step12: run while if tracking_state if"

                    self.tracking_state = 0
                    self.start()
                    motor.walk(0, 0)
                else:
                    print "Step13: run while if tracking_state else"

                    if self.show_backproj:
                        vis[:] = prob[..., np.newaxis]
                    try:
                        print "Step14: run while if tracking_state try"

                        cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                        #                         print track_box
                        imageX = track_box[0][0]
                        imageY = track_box[0][1]
                        imageR = (track_box[1][0] + track_box[1][1]) / 4
                        visCut = vis[int(imageY - imageR):int(imageY + imageR),
                                     int(imageX - imageR):int(imageX + imageR)]
                        imageSize = (int(100), int(100))
                        visResize = cv2.resize(visCut,
                                               imageSize,
                                               interpolation=cv2.INTER_AREA)
                        visResize = cv2.medianBlur(visResize, 5)
                        visResizeHSV = cv2.cvtColor(visResize,
                                                    cv2.COLOR_BGR2HSV)

                        #hsvWidth = visResizeHSV.width
                        #hsvHeight = visResizeHSV.height
                        #for i in range(0, hsvWidth):
                        #    for j in range(0, hsvHeight):
                        #hsvTmp = cv2.cvGet2D(visResizeHSV, i, j)
                        # hsvTmp = visResizeHSV[i, j]
                        #hsvPoint = NULL
                        #if not (hsvTmp[0] > 90 and hsvTmp[0] < 120):
                        #hsvPoint.val[0] = 0
                        #hsvPoint.val[1] = 0
                        #hsvPoint.val[2] = 0
                        #cv2.cvSet2D(visResizeHSV, i, j, hsvPoint)
                        # hsvTmp[0] = 0
                        # hsvTmp[1] = 0
                        # hsvTmp[2] = 0
                        #  visResizeHSV[i, j] = hsvTmp

                        print "visCut before"
                        #visCut = vis[0:100, 0:100]
                        cv2.imshow('imageCut', visCut)
                        cv2.imshow('imageResize', visResize)
                        #cv2.imshow('imageResize', visResizeHSV)
                        print "visCut after"
                        imCount = imCount + 1
                        imName = "./right/" + str(imCount) + ".jpg"
                        #print "imPath = " + imPath
                        #cv2.imwrite("imPath", visResize)
                        cv2.imwrite(imName, visResize)

                        a = str(track_box[0][0])+" "+str(track_box[0][1])+" "+str(round(track_box[1][0],2))\
                                       +" "+str(round(track_box[1][1],2))+" "+str(round(track_box[2],2))+"\r\n"
                        print a

                        ActualCenterX = track_box[0][0]
                        print "try1"
                        TargetCentorX = 160
                        print "try2"
                        SetX = self.PID_Center(ActualCenterX, TargetCentorX)

                        ActualForward = (track_box[1][0] + track_box[1][1]) / 2
                        TargetForward = 80
                        if ActualForward < 80:
                            ActualForward = 1.732 * ActualForward - 58.56

                        SetF = self.PID_Forward(ActualForward, TargetForward)

                        #imageX = track_box[0][0]
                        #imageY = track_box[0][1]
                        #imageR = (track_box[1][0] + track[1][1]) / 2
                        #visCut = vis[imageX:imageY, imageR:imageR]
                        #visCut = vis[50:50, 100:100]
                        #cv2.imshow('imageCut', visCut)

                        cv2.ellipse(vis, track_box, (0, 0, 255), 2)

                        print "try3"

                        #motor.walk(-SetX+SetF, SetX+SetF, 5)
                        #motor.walk(SetF, SetF)

                        print "try4"
                        #b = "AcutualX = "+str(ActualCenterX)+" TargetX = "+str(TargetCentorX)+" SetX = "+str(SetX)
                        #print b
                        #print SetX


#                         self.ser.write(a)
                    except:
                        traceback.print_exc()
                        #print "Error"
                        #print track_box

            cv2.imshow('camshift', vis)
            # cv2.imshow('imageCut', visCut)

            ch = 0xFF & cv2.waitKey(5)
            if ch == 27:
                break
            if ch == ord('b'):
                self.show_backproj = not self.show_backproj
            if ch == ord('r'):
                self.tracking_state = 0
                self.start()
        cv2.destroyAllWindows()
Ejemplo n.º 25
0
def main():

    global frame, roiPts, inputMode

    camera = cv2.VideoCapture(0)

    cv2.namedWindow("frame")
    cv2.setMouseCallback("frame", selectROI)

    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None

    while True:

        (grabbed, frame) = camera.read()

        if roiBox is not None:

            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            backProj = cv2.calcBackProject([hsv], [0], roiHist, [0, 180], 1)

            (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
            pts = np.int0(cv2.cv.BoxPoints(r))

            #coordinates contain the coordinates of the tracked object
            coordinates = r[0]
            # x, y contains the coordinates
            x = int(coordinates[0])
            y = int(coordinates[1])
            #print ('x = '+ str(x))
            #print ('y = '+ str(y))
            #move(x,y)
            #mouse controlling actions
            py.moveTo(int(x * 2.5), int(y * 2))

            #draws a circle around the center from x,y
            cv2.circle(frame, (int(x), int(y)), 4, (0, 0, 255), 2)
            #draws a colored frame around the object
            cv2.polylines(frame, [pts], True, (0, 255, 0), 2)

        cv2.imshow("frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # handle if the 'i' key is pressed, then go into ROI
        if key == ord("i") and len(roiPts) < 4:

            inputMode = True
            orig = frame.copy()

            while len(roiPts) < 4:
                cv2.imshow("frame", frame)
                cv2.waitKey(0)

            roiPts = np.array(roiPts)
            s = roiPts.sum(axis=1)
            tl = roiPts[np.argmin(s)]
            br = roiPts[np.argmax(s)]

            roi = orig[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

            roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180])
            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)
            roiBox = (tl[0], tl[1], br[0], br[1])

        # if the 'q' key is pressed, stop the loop
        elif key == ord("q"):
            break

    camera.release()
    cv2.destroyAllWindows()
Ejemplo n.º 26
0
def cam_tracker(v, file_name):
    # Open output file
    output_name = sys.argv[3] + file_name
    output = open(output_name, "w")

    frameCounter = 0
    # read first frame
    ret, frame = v.read()
    if ret == False:
        return

    # detect face in first frame
    c, r, w, h = detect_one_face(frame)

    pt = (frameCounter, (c + w / 2), (r + h / 2))
    # Write track point for first frame
    output.write("%d,%d,%d\n" % pt)  # Write as 0,pt_x,pt_y
    frameCounter = frameCounter + 1

    # set the initial tracking window
    track_window = (c, r, w, h)

    # calculate the HSV histogram in the window
    # NOTE: you do not need this in the Kalman, Particle or OF trackers
    roi_hist = hsv_histogram_for_window(frame, (c, r, w,
                                                h))  # this is provided for you

    # initialize the tracker
    # e.g. kf = cv2.KalmanFilter(4,2,0)
    # or: particles = np.ones((n_particles, 2), int) * initial_pos

    term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    while (1):
        ret, frame = v.read()  # read another frame
        if ret == False:
            break

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        ret, track_window = cv2.CamShift(dst, track_window, term_crit)

        (c, r, w, h) = track_window
        pt = (frameCounter, (c + w / 2), (r + h / 2))

        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame, [pts], True, 255, 2)
        #cv2.imshow('img2',img2)
        #cv2.waitKey(40)
        # e.g. cv2.meanShift, cv2.CamShift, or kalman.predict(), kalman.correct()

        # use the tracking result to get the tracking point (pt):
        # if you track a rect (e.g. face detector) take the mid point,
        # if you track particles - take the weighted average
        # the Kalman filter already has the tracking point in the state vector

        # write the result to the output file
        output.write("%d,%d,%d\n" % pt)  # Write as frame_index,pt_x,pt_y
        frameCounter = frameCounter + 1

    output.close()
Ejemplo n.º 27
0
        mask = cv2.inRange(hsv_roi, np.array((150., 150., 50.)), np.array((190., 255., 255.)))

        roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])

        cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)

        term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        cv2.imshow('back',dst)

        ret, track_window = cv2.CamShift(dst, track_window, term_crit)

        # Draw it on image
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img = cv2.polylines(frame, [pts], True, 255, 2)

        cv2.circle(img, (int(c+w/2),int(r+h/2)),3, [0,255,0])



       #x, y, w, h = track_window
       # img2 = cv2.rectangle(frame, (x, y), (x + w, y + h), 255, 2)


        cv2.imshow('img',img)
    def run(self):
        try:
            while True:
                self.frame = cv2.cvtColor(self.drone.get_image(), cv2.COLOR_BGR2RGB)
                vis = self.frame.copy()
                hsv = cv2.cvtColor(self.frame, cv2.COLOR_RGB2HSV)
                mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))

                if self.selection:
                    x0, y0, x1, y1 = self.selection
                    self.track_window = (x0, y0, x1-x0, y1-y0)
                    hsv_roi = hsv[y0:y1, x0:x1]
                    mask_roi = mask[y0:y1, x0:x1]
                    hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
                    cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX);
                    self.hist = hist.reshape(-1)
                    self.show_hist()

                    vis_roi = vis[y0:y1, x0:x1]
                    cv2.bitwise_not(vis_roi, vis_roi)
                    vis[mask == 0] = 0

                if self.tracking_state == 1:
                    try:
                        self.selection = None
                        prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                        prob &= mask
                        term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
                        track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit)

                        if self.show_backproj:
                            vis[:] = prob[...,np.newaxis]
                        try: cv2.ellipse(vis, track_box, (0, 0, 255), 2)
                        except: print track_box
                        x_distance = 2*(track_box[0][0] / hsv.shape[1]) - 1
                        x_speed = 0.5 * x_distance
                        y_distance = 2*(track_box[0][1] / hsv.shape[0]) - 1
                        y_speed = -0.2 * y_distance
                        z_distance = np.min([(track_box[1][0] / hsv.shape[0]), (track_box[1][1] / hsv.shape[0])]) - 0.4 # Both sizes relative to y of image
                        z_speed = -0.3 * z_distance
                        #print (x_speed, y_speed, z_speed)
                        #print track_box

                        x_speed = x_speed * self.speed_multiplier
                        y_speed = y_speed * self.speed_multiplier
                        z_speed = z_speed * self.speed_multiplier

                        if self.ai_control:
                            self.drone.at(libardrone.at_pcmd, True, 0, -z_speed, y_speed, x_speed)
                    except:
                        print 'Tracking failed'
                        self.ai_control = False
                        self.drone.hover()

                cv2.imshow('camshift', vis)

                #ch = 0xFF & cv2.waitKey(5)
                ch = cv2.waitKey(5)
                if ch == 27:
                    self.drone.emergency()
                if ch == ord('q'):
                    break
                if ch == ord('b'):
                    self.show_backproj = not self.show_backproj
                if ch == ord('\r'):
                    self.drone.takeoff()
                if ch == ord(' '):
                    self.drone.land()                
                if ch == ord('a'):
                    self.drone.move_left()
                    self.ai_control = False
                if ch == ord('d'):
                    self.drone.move_right()
                    self.ai_control = False
                if ch == ord('w'):
                    self.drone.move_forward()
                    self.ai_control = False
                if ch == ord('s'):
                    self.drone.move_backward()
                    self.ai_control = False
                if ch == ord('9'):
                    self.speed_multiplier = self.speed_multiplier * 1.1
                if ch == ord('8'):
                    self.speed_multiplier = self.speed_multiplier * 0.9
                if ch == 63234:
                    self.drone.turn_left(mult=2.5)
                    self.ai_control = False
                if ch == 63235:
                    self.drone.turn_right(mult=2.5)
                    self.ai_control = False
                if ch == 63232:
                    self.drone.move_up()
                    self.ai_control = False
                if ch == 63233:
                    self.drone.move_down()
                    self.ai_control = False
                if (ch == ord('z')) or (ch == ord('x')) or (ch == ord('c')) or (ch == ord('v')): # i.e. can mash keyboard
                    self.drone.hover()
                    self.ai_control = False
                if ch == ord('y'):
                    self.drone.trim()
                if ch == ord('p'):
                    self.drone.reset()
                if ch == ord('t'):
                    self.ai_control = not self.ai_control
                    if self.ai_control:
                        print 'AI in control'
                    else:
                        self.drone.hover()
                        print 'No AI control'
        finally:
            cv2.destroyAllWindows()
            self.drone.emergency()
            self.drone.reset()
            self.drone.halt()
# 获取ROI直方图
roi = frame[y:y + h, x:x + w]
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv_roi, (26, 43, 46), (34, 255, 255))
roi_hist = cv.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)

# 设置搜索跟踪分析
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)
while True:
    ret, frame = cap.read()
    if ret is False:
        break
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

    # ,搜索更新roi区域
    track_box = cv.CamShift(dst, track_window, term_crit)
    track_window = track_box[1]
    print(track_box)
    # 绘制窗口CAM
    cv.ellipse(frame, track_box[0], (0, 0, 255), 3, 8)
    cv.imshow('CAM Demo', frame)
    k = cv.waitKey(50) & 0xff
    if k == 27:
        break
    else:
        cv.imwrite(chr(k) + ".jpg", frame)

cv.destroyAllWindows()
cap.release()
Ejemplo n.º 30
0
def main():
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="path to the (optional) video file")
    args = vars(ap.parse_args())

    # grab the reference to the current frame, list of ROI
    # points and whether or not it is ROI selection mode
    global frame, roiPts, inputMode

    # if the video path was not supplied, grab the reference to the
    # camera
    if not args.get("video", False):
        camera = cv2.VideoCapture(0)

    # otherwise, load the video
    else:
        camera = cv2.VideoCapture(args["video"])

    # setup the mouse callback to grab the clicks to define ROI
    cv2.namedWindow("frame")
    cv2.setMouseCallback("frame", selectROI)

    # initialize the termination criteria for cam shift, indicating
    # a maximum of ten iterations or movement by a least one pixel
    # along with the bounding box of the ROI
    termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)
    roiBox = None

    # keep looping over the frames
    while True:
        # grab the current frame
        (grabbed, frame) = camera.read()

        # resize frame
        frame = imutils.resize(frame, width=800)
        # check to see if we have reached the end of the video
        if not grabbed:
            break

        # if the see if the ROI has been computed
        if roiBox is not None:
            # convert the current frame to the HSV color space
            # and perform mean shift
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            # optional sharpening of the image: blur with minimal kernel then
            # weighted subtract from image
            # image = cv2.GaussianBlur(hsv, (0, 0), 3)
            # hsv = cv2.addWeighted(hsv, 1.5, image, -0.5, 0, image)

            # 2D histogram back-projection (Hue + Saturation)
            backProj = cv2.calcBackProject([hsv], [0, 1], roiHist,
                                           [0, 179, 0, 255], 1)

            # Show back-projection image result
            # cv2.imshow("backProjection", backProj)

            # apply cam shift to the back projection, convert the
            # points to a bounding box, and then draw them
            (r, roiBox) = cv2.CamShift(backProj, roiBox, termination)
            pts = np.int0(cv2.boxPoints(r))
            cv2.polylines(frame, [pts], True, (0, 255, 0), 2)

        # show the frame and record if the user presses a key
        cv2.imshow("frame", frame)
        key = cv2.waitKey(1) & 0xFF

        # handle if the 'i' key is pressed, then go into ROI
        # selection mode
        if key == ord("i") and len(roiPts) < 4:
            # indicate that we are in input mode and clone the
            # frame (to keep original since we later write on it.)
            inputMode = True
            orig = frame.copy()

            # keep looping until 4 reference ROI points have
            # been selected; press any key to exit ROI selction
            # mode once 4 points have been selected
            while len(roiPts) < 4:
                cv2.imshow("frame", frame)
                cv2.waitKey(0)

            # determine the top-left and bottom-right points
            # Top Left is the minimum if the sum, Bottom Right is the max
            # (this is true for a somewhat rectangular shape)
            roiPts = np.array(roiPts)
            s = roiPts.sum(axis=1)
            tl = roiPts[np.argmin(s)]
            br = roiPts[np.argmax(s)]

            # store bounding box
            roiBox = (tl[0], tl[1], br[0], br[1]
                      )  # grab the ROI for the bounding box and convert it

            # crop image and convert to the HSV color space (Hue-Saturation-Value)
            roi = orig[tl[1]:br[1], tl[0]:br[0]]
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
            # optional blurring to smooth the histogram
            roi = cv2.GaussianBlur(roi, (21, 21), 0)

            # show histogram
            # cv2.imshow("histogram", roi)

            # compute a HSV histogram for the ROI

            # 2D histogram calculation, with channel 0 (H-Hue) and 1 (S-Saturation)
            # in range 0-180 degrees for Hue and 0-255 for Saturation
            roiHist = cv2.calcHist([roi], [0, 1], None, [6, 8],
                                   [0, 180, 0, 255])

            # normalize histogram to 0-255 uint8 space
            roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX)

        # if the 'q' key is pressed, stop the loop
        elif key == ord("q"):
            break

    # cleanup the camera and close any open windows
    camera.release()
    cv2.destroyAllWindows()