Example #1
0
    def handle_state(self, times, other):
        common.add_to_array(self.cm_array, self.center_of_mass)
        common.add_to_array(self.ap_array, self.action_point)
        common.add_to_array(self.height_array, imcompare.find_contour_height(self.contour))

        if len(times) == common.ARRAY_LEN:
            xAP, yAP, vAP, vH, h = imcompare.get_motion_params(self.cm_array, self.ap_array, self.height_array, times)
            state = imcompare.get_current_state(xAP, yAP, vAP, vH, h, self.move_state, self.crouch_height, self.hit_distance, self.hit_speed, self.high_hit_height)
            if self.move_state != state:
                self.previous_move_states += '-' + state
                self.make_move(other)
                self.move_state = state
Example #2
0
def main():

    g_slider_pos = 45
    g_vertex_pos = 100
    g_width_pos = 2
    arr_len = 5
    global CROUCH_RATIO, HIT_DISTANCE_RATIO, HIT_SPEED_RATIO, HIGH_HIT_RATIO
    CROUCH_RATIO = 0.9
    HIT_DISTANCE_RATIO = 0.25
    HIT_SPEED_RATIO = 0.50
    HIGH_HIT_RATIO = 1.1
    
    cv.NamedWindow("win") # don't know if a '?' in the name is supported    
    cv.MoveWindow("win", -30, -20)


    cap = cv.CaptureFromCAM(0)

    calib_img = cv.LoadImage(r'images\Calibration_Grid3.png')
    cv.ShowImage("win", calib_img)

    # this is actually the dimensions of the "inner chessboard" (see FindChessboardCorners documentation)
    chessboard_dim = ( 8, 5 )

    # The camera's auto-focus needs time to adjust
    cv.WaitKey(5000)
    # For some reason the first capture doesn't get the chessboard (even though it's there 5 secs!) and the second one sometimes comes out blurry.
    whole_view = cv.QueryFrame(cap)
    whole_view = cv.QueryFrame(cap)

    # converting our image to grayscale. Might be unnecessary.
    whole_view_gs = cv.CreateImage(cv.GetSize(whole_view), whole_view.depth, 1)
    cv.CvtColor(whole_view, whole_view_gs, cv.CV_BGR2GRAY)
    found_all, corners = cv.FindChessboardCorners( whole_view_gs, chessboard_dim )

    if found_all:
        cv.DrawChessboardCorners( whole_view, chessboard_dim, corners, found_all )
    
    cv.ShowImage("win", whole_view);
    key = cv.WaitKey()

    # these are the bounding 4 points of the inner chessboard.
    bounding_rect = (corners[39], corners[32], corners[7], corners[0])
    
    transform_mat = cv.CreateMat(3, 3, cv.CV_32F)
    cv.GetPerspectiveTransform(bounding_rect, screen_rect, transform_mat)

    cv.WarpPerspective(whole_view, calib_img, transform_mat)

    cv.ShowImage("win", calib_img)

    cv.CreateTrackbar("Crouch", "win", CROUCH_RATIO * 100, 100, standHeight_change)
    cv.CreateTrackbar("HitDistance", "win", HIT_DISTANCE_RATIO * 100, 100, hitDis_change)
    cv.CreateTrackbar("HitSpeed", "win", HIT_SPEED_RATIO * 100, 100, hitV_change)
    cv.CreateTrackbar("HighHit", "win", HIGH_HIT_RATIO * 100, 200, highHitDis_change)
    cv.WaitKey()
    cv.Set(calib_img, cv.CV_RGB(255, 255, 255))
##    cv.ShowImage("win", calib_img)
##    cv.WaitKey()
    reference_img = cv.QueryFrame(cap)
    reference_img = cv.QueryFrame(cap)
    reference_img_warped = cv.CreateImage(cv.GetSize(calib_img), 8, 3)
    cv.WarpPerspective(reference_img, reference_img_warped, transform_mat, 0)
        
    cv.SaveImage('RefImg/RefImage2.jpg',reference_img_warped)
    reference_img_warped = cv.LoadImage('RefImg/RefImage2.jpg')
    cv.ShowImage("win", reference_img_warped)    

    cv.WaitKey()
    
    feed = MyFeed(cap)
    feed.start()

    prev_state = state = 'Idle'
    cms = []
    aps = []
    hs = []
    times = []
    base_height = 0
    while True :
        frame = None
        global pixBuf,newPix,waitPix,lock
        if newPix:
            lock.acquire()
            frame = pixBuf
            # Tell next frameStarted that this image was already taken into account
            newPix = False
            # Tell thread to acquire a new image
            waitPix = True
            lock.release()
        #frame = cv.QueryFrame(cap)
        #cv.ShowImage("win2", im)
        # warp the image to include only the synced part
        if frame:
            warped_frame = cv.CreateImage(cv.GetSize(calib_img), 8, 3)
            cv.WarpPerspective(frame, warped_frame, transform_mat, 0)        

            (c1, cm1, c2, cm2) = imcompare.findDifference(reference_img_warped, warped_frame, g_slider_pos, g_width_pos, g_vertex_pos)
            if c1 and c2:
                (ap1,ap2) = imcompare.get_action_points(c1, c2)

                cms = add_to_array(cms, cm1, arr_len)
                aps = add_to_array(aps, ap1, arr_len)
                hs = add_to_array(hs, imcompare.find_contour_height(c1), arr_len)
                times = add_to_array(times, time.time(), arr_len)
                warped_frame = imcompare.drawDifference(cv.GetSize(warped_frame),c1, cm1, ap1, c2, cm2, ap2)

                if len(cms) == arr_len:
                    xAP, yAP, vAP, vH, h = imcompare.get_motion_params(cms, aps, hs, times)
                    standHeight = base_height * CROUCH_RATIO
                    hitDis = base_height * HIT_DISTANCE_RATIO
                    hitV = base_height * HIT_SPEED_RATIO
                    highHitDis = base_height * HIGH_HIT_RATIO
                    state = imcompare.get_current_state(xAP, yAP, vAP, vH, h, prev_state, standHeight, hitDis, hitV, highHitDis)
                    if prev_state != state:
                        print state
                    prev_state = state

            cv.ShowImage('win', warped_frame)
            key = cv.WaitKey(10)
            if  key == 27:
                break
            if key == 32:
                if c1:
                    base_height = imcompare.find_contour_height(c1)
                    print base_height
    feed.stop()