Example #1
0
    def run(self):

        game_clock = pygame.time.Clock()

        camera_feed = common.CameraFeed(self.capture, self.transform_mat)
        camera_feed.start()

        self.screen.blit(self.background, (0,0))
        pygame.display.flip()

        while self.state_running:
            for event in pygame.event.get():
                self._process_event(event)
                if event.type == KEYUP and event.key == K_TAB:
                    self.state_running = False
                    break
                if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
                    camera_feed.stop()
                    raise InterruptedException()
            
            self.frame = None
            if camera_feed.new_pic_available:
                camera_feed.lock.acquire()
                self.frame = camera_feed.ready_img
                camera_feed.new_pic_available = False
                camera_feed.wait_for_pic = True
                camera_feed.lock.release()
            if not self.frame:
                continue
            self.warped_frame = cv.CreateImage(common.RESOLUTION, 8, 3)
            cv.WarpPerspective(self.frame, self.warped_frame, self.transform_mat, 0)

            self.backup_warped_frame = cv.CreateImage(common.RESOLUTION, 8, 3)            
            cv.Copy(self.warped_frame, self.backup_warped_frame)
            # make the sprites we changed not visible to the image processing
            if len(self.sprites) > 0:
                for sprite in self.sprites:
                    if self.screen.get_rect().inflate(sprite.rect.width, sprite.rect.height).contains(sprite.rect):
                        r = (sprite.rect.x if sprite.rect.x >= 0 else 0,
                             sprite.rect.y if sprite.rect.x >= 0 else 0,
                             sprite.rect.width if sprite.rect.width + sprite.rect.x <= common.RESOLUTION[0] else common.RESOLUTION[0] - sprite.rect.x,
                             sprite.rect.height if sprite.rect.height + sprite.rect.y <= common.RESOLUTION[1] else common.RESOLUTION[1] - sprite.rect.y)
                        cv.SetImageROI(self.warped_frame, r)
                        cv.SetImageROI(self.ref_img, r)
                        cv.Copy(self.ref_img, self.warped_frame)
                cv.ResetImageROI(self.warped_frame)
                cv.ResetImageROI(self.ref_img)

            # Except for those we WANT to be
            if len(self.special_detection_rects) > 0:
                for rect in self.special_detection_rects:
                    r = (rect.x, rect.y, rect.width, rect.height)
                    cv.SetImageROI(self.backup_warped_frame, r)
                    cv.SetImageROI(self.warped_frame, r)
                    cv.Copy(self.backup_warped_frame, self.warped_frame)
                cv.ResetImageROI(self.warped_frame)
                cv.ResetImageROI(self.ref_img)

                                        


            c1, cm1, c2, cm2 = imcompare.findDifference(self.ref_img, self.warped_frame, common.THRESHOLD, common.WIDTH_LIM, common.VERTEX_COUNT)

            ap1, ap2 = imcompare.get_action_points(c1, c2)

            if self.should_draw_bounding_box or self.redraw_next_frame:
                self.screen.blit(self.background, (0,0))

            self.rects = []                

            self._state_specific()
            self.sprites.update(pygame.time.get_ticks())

            if c1:
                self.process_player_one(c1, cm1, ap1)
            if c2:
                self.process_player_two(c2, cm2, ap2)

            self.process_attacks()                

            if common.SHOW_FPS:
                s = "FPS = %f" % game_clock.get_fps()
                text = self.font.render(s, 1, (10, 10, 10), (255, 255, 255))
                self.rects.append(self.screen.blit(text, (0, 0)))

            self.rects += self.sprites.draw(self.screen)


            if self.should_draw_bounding_box or self.redraw_next_frame:
                pygame.display.flip()
                self.redraw_next_frame = False
            else:
                pygame.display.update(self.rects)
            game_clock.tick(common.MAX_FPS)
            self.sprites.clear(self.screen, self.background)

        camera_feed.stop()
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()