def getCamFrame(color, camera): retval, frame = camera.read() frame = imutils.resize(frame, width=700) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if not color: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB) frame = numpy.rot90(frame) frame = pygame.surfarray.make_surface(frame) return frame
def getWebcam(screen, clock, WIDTH, HEIGHT): camera = cv2.VideoCapture(0) done = False while not done: ret, frame = camera.read() for event in pygame.event.get(): if event.type == pygame.QUIT: return 0 elif event.type == pygame.KEYDOWN: img_name = "user.jpg".format(0) cv2.imwrite(img_name, frame) return "user.jpg" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = numpy.rot90(frame) frame = pygame.surfarray.make_surface(frame) frame = pygame.transform.scale(frame, (WIDTH, HEIGHT)) screen.blit(frame, (0, 0)) pygame.draw.rect(screen, WHITE, (0, 0, 120, 20), 0) font = pygame.font.SysFont('Calibri', 20, True, False) tips = font.render("Press any Key", True, BLACK) screen.blit(tips, [3, 3]) pygame.display.update() clock.tick(30)
def game_main(): global last_comment_time global profilePicturesDict global done get_rules() comments = [] robot1_commands = [] robot2_commands = [] robot1_to_firebase = [] robot2_to_firebase = [] final_commands = [] pending_commands = [] phase1 = -1 phase2 = -1 #cam = pygame.camera.Camera(pygame.camera.list_cameras()[0]) active = {} start_loading_comments(last_post, comments, last_comment_time) #start_pushing_commands(robot1_to_firebase, 'https://fbhack-9a7bd.firebaseio.com/robot1.json') #start_pushing_commands(robot2_to_firebase, 'https://fbhack-9a7bd.firebaseio.com/robot2.json') start_pushing_commands(final_commands) while not done: #img = cam.get_image() #img = pygame.transform.scale(img, (WIDTH, HEIGHT)) (grabbed, frame) = camera.read() frame = imutils.resize(frame, width=WIDTH, height=HEIGHT) img = pygame.image.frombuffer(frame.tostring(), frame.shape[1::-1], "RGB") screen.blit(img, (0, 0)) screen.blit(background, (0, 400)) pygame.draw.circle(screen, (200, 0, 0), (WIDTH / 2, HEIGHT / 2), (HEIGHT - 20) / 2, 5) new_comments = list(comments) [comments.pop() for i in range(len(comments))] for comment in new_comments: user = comment['from']['id'] content = comment['message'] pending_commands.append((user, content)) new_pending_commands = [] for pending in pending_commands: user = pending[0] content = pending[1] if user not in profilePicturesDict: getProfilePicAsync(user) new_pending_commands.append(pending) elif profilePicturesDict[user] == 'dummy': new_pending_commands.append(pending) else: parse_command(pending, robot1_commands, robot2_commands, final_commands) #parse_command(pending, robot1_commands, robot2_commands, robot1_to_firebase, robot2_to_firebase) pending_commands = new_pending_commands if (phase1 == PHASE_UPDATE - 1): if (len(robot1_commands) > 0): robot1_commands.pop(0) if (phase2 == PHASE_UPDATE - 1): if (len(robot2_commands) > 0): robot2_commands.pop(0) if (phase1 >= 0): phase1 = (phase1 + 1) % PHASE_UPDATE if (phase2 >= 0): phase2 = (phase2 + 1) % PHASE_UPDATE if (len(robot1_commands) > 0): command = robot1_commands[0] screen.blit(profilePicturesDict[command[0]], CONTROLLER1) if (phase1 < 0): phase1 = 0 else: phase1 = -1 if (len(robot2_commands) > 0): command = robot2_commands[0] screen.blit(profilePicturesDict[command[0]], CONTROLLER2) if (phase2 < 0): phase2 = 0 else: phase2 = -1 robot1_queue = map(lambda command: command[0], robot1_commands[1:min(5, len(robot1_commands))]) x = 10 y = 495 - PIC_SIZE for user in robot1_queue: curr_img = pygame.transform.scale(profilePicturesDict[user], (SM_PIC_SIZE, SM_PIC_SIZE)) y -= SM_PIC_SIZE screen.blit(curr_img, (x, y)) robot2_queue = map(lambda command: command[0], robot2_commands[1:min(5, len(robot2_commands))]) x = WIDTH - SM_PIC_SIZE y = 495 - PIC_SIZE for user in robot2_queue: curr_img = pygame.transform.scale(profilePicturesDict[user], (SM_PIC_SIZE, SM_PIC_SIZE)) y -= SM_PIC_SIZE screen.blit(curr_img, (x, y)) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: active[event.key] = True elif event.type == pygame.KEYUP and event.key in active: del active[event.key] elif event.type == pygame.QUIT: done = True for key in active.keys(): if key == pygame.K_ESCAPE: done = True elif key == pygame.K_r: get_rules() pygame.display.flip() clock.tick(FPS)
import cv2 camera_port = 0 ramp_frames = 30 camera = cv2.VideoCapture(camera_port) i=1 while True : retval, im = camera.read() cv2.imshow("face",im) if cv2.waitKey(1) & 0xFF == ord('q'): break #photo=pygame.image.save(img, 'dataset_image/1/image'+str(i)+'.jpg') try: if(i<=50): resized_image = cv2.resize(im, (64, 64)) cv2.imwrite('dataset_image/5/image'+str(i)+'.jpg', resized_image) i=i+1 else: break
def game(): global count_score #-------------- part opencv --------------# camera = cv2.VideoCapture(0) face_detector = cv2.CascadeClassifier(r'C:\Users\User\workspace-software\Game\OpenCV\haarcascade_frontalface_default.xml') camera.set(3, 1280) camera.set(4, 720) #-----------------------------------------# surface1 = pygame.Surface( screen.get_size(), pygame.SRCALPHA ) obj_ball = ball(surface1) #สร้างบอลลูกแรก count_score = 0 #Timer font = pygame.font.SysFont(None, 45) counter = 60 text = font.render(str(counter), True, white) timer_event = pygame.USEREVENT+1 pygame.time.set_timer(timer_event, 1000) text_rect = text.get_rect() text_rect.center = (150,15) running= True while running: #------------------- part openCV ----------------# ret, frame = camera.read() frame = cv2.resize(frame, (1280, 720)) frame = cv2.flip(frame,1) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.transpose(frame) img = pygame.surfarray.make_surface(frame) # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_detector.detectMultiScale(gray, 1.1, 4) # Draw the rectangle around each face for (x, y, w, h) in faces: pygame.draw.rect(img, (0,255,0), (x,y,x+w, y+h), 3) print((x,y)) if obj_ball.is_collided(x, y): surface1 = pygame.Surface( screen.get_size(), pygame.SRCALPHA ) #เหมือนการรีหน้า surface ใหม่เพื่อลบบอลออก count_score += 1 pygame.time.wait(50) #รอเวลาสักครู้เพื่อขึ้นลูกใหม่ obj_ball = ball(surface1) #------------------------------------------------# screen.blit( img , (0,0) ) pygame.draw.rect(screen, black , (0,0,1280,60)) create_text('TIME : ' ,80,30) screen.blit(text, text_rect.center) create_text('SCORE : ' + str(count_score) ,1150,30) mx2, my2 = pygame.mouse.get_pos() button_2 = pygame.Rect(1180,670,100,50) #(left,top,width,hight) pygame.draw.rect(screen, red , button_2) create_text('EXIT',1230,700) if button_2.collidepoint((mx2, my2)): if click: score() click = False for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == timer_event: counter -= 1 text = font.render(str(counter), True, white) if counter == 0: pygame.time.set_timer(timer_event, 0) #time_up() camera.release() cv2.destroyAllWindows() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() camera.release() cv2.destroyAllWindows() if event.type == MOUSEBUTTONDOWN: if event.button == 1: click = True if event.type == pygame.MOUSEBUTTONDOWN: if obj_ball.is_collided(mx2, my2): surface1 = pygame.Surface( screen.get_size(), pygame.SRCALPHA ) #เหมือนการรีหน้า surface ใหม่เพื่อลบบอลออก count_score += 1 pygame.time.wait(50) #รอเวลาสักครู้เพื่อขึ้นลูกใหม่ obj_ball = ball(surface1) screen.blit( surface1 , (0,0) ) pygame.display.update() clock.tick(60)
def get_image(): global camera retval,im = camera.read() return im