M0n5t3r

Untitled

Apr 24th, 2019
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3. import time
  4. import easingScripts
  5. import sys
  6. from sprite import Player
  7. import face_recognition
  8. import cv2
  9. import re
  10.  
  11. worldx = 1920
  12. worldy = 1080
  13.  
  14. fps = 100        # frame rate
  15. clock = pygame.time.Clock()
  16. pygame.init()
  17. main = True
  18.  
  19. video_capture = cv2.VideoCapture(0)
  20.  
  21.  
  22. cap_width = video_capture.get(3)
  23. cap_height = video_capture.get(4)
  24. print(cap_width, cap_height)
  25. video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
  26. video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
  27.  
  28. BLACK = (23, 23, 23)
  29. ALPHA = (0, 255, 0)
  30.  
  31. world = pygame.display.set_mode([worldx, worldy], pygame.FULLSCREEN)
  32.  
  33. player = Player()   # spawn player
  34. player.rect.x = 0
  35. player.rect.y = 0
  36. player_list = pygame.sprite.Group()
  37. player_list.add(player)
  38.  
  39. new_x_pos = []
  40. new_y_pos = []
  41.  
  42. face_locations = []
  43. face_encodings = []
  44. face_names = []
  45. process_this_frame = True
  46.  
  47. '''
  48. Main loop
  49. '''
  50. animation_counter = 1
  51. while main == True:
  52.     ret, frame = video_capture.read()
  53.     cv2.imshow('frame', frame)
  54.     # Resize frame of video to 1/4 size for faster face recognition processing
  55.     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  56.  
  57.     # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  58.     rgb_small_frame = small_frame[:, :, ::-1]
  59.     if process_this_frame:
  60.         # Find all the faces and face encodings in the current frame of video
  61.         face_locations = face_recognition.face_locations(rgb_small_frame)
  62.         for i in range(len(face_locations)):
  63.             something = str(face_locations[i])
  64.             print(something)
  65.             # get facelocations in a procentage.
  66.             something = re.sub('[()]', "", something)
  67.             something = something.replace(" ", "")
  68.             list_something = something.split(",")
  69.            
  70.             avarage_y = (int(list_something[0])*4) + (int(list_something[2])*4) / 2
  71.             avarage_x = (int(list_something[1])*4) + (int(list_something[3])*4) / 2
  72.            
  73.             avarage_procent_y = 100/cap_width*avarage_y
  74.             avarage_procent_x = 100/cap_height*avarage_x
  75.             print(avarage_procent_y, avarage_procent_x)
  76.             new_target_x = avarage_x
  77.             new_target_y = avarage_y
  78.  
  79.             # if (float(player.rect.x) != float(new_target_x)) or (float(player.rect.y) != float(new_target_y)):
  80.             #             target_x = float(new_target_x)
  81.             #             target_y = float(new_target_y)
  82.  
  83.             #             position_x = float(player.rect.x)
  84.             #             position_y = float(player.rect.y)
  85.             #             for q in range(0, fps):
  86.             #                 p = q + 1
  87.             #                 new_x_pos.append(easingScripts.easeInOutQuint(
  88.             #                     1, position_x, target_x-position_x, p))
  89.             #                 new_y_pos.append(easingScripts.easeInOutQuint(
  90.             #                     1, position_y, target_y-position_y, p))
  91.  
  92.             #             new_x_pos.reverse()
  93.             #             new_y_pos.reverse()
  94.  
  95.     for event in pygame.event.get():
  96.         if event.type == pygame.QUIT:
  97.             pygame.quit()
  98.             sys.exit()
  99.             main = False
  100.  
  101.         if event.type == pygame.KEYDOWN:
  102.             if event.key == pygame.K_z or event.key == ord('z'):
  103.                 if new_y_pos == [] and new_x_pos == []:
  104.                     given_x = randint(0, worldx-player.rect.width)
  105.                     given_y = randint(0, worldy-player.rect.height)
  106.                     if (float(player.rect.x) != float(given_x)) or (float(player.rect.y) != float(given_y)):
  107.                         target_x = float(given_x)
  108.                         target_y = float(given_y)
  109.  
  110.                         position_x = float(player.rect.x)
  111.                         position_y = float(player.rect.y)
  112.                         for q in range(0, fps):
  113.                             p = q + 1
  114.                             new_x_pos.append(easingScripts.easeInOutQuint(
  115.                                 1, position_x, target_x-position_x, p))
  116.                             new_y_pos.append(easingScripts.easeInOutQuint(
  117.                                 1, position_y, target_y-position_y, p))
  118.  
  119.                         new_x_pos.reverse()
  120.                         new_y_pos.reverse()
  121.  
  122.             if event.key == ord('q'):
  123.                 pygame.quit()
  124.                 sys.exit()
  125.                 main = False
  126.  
  127.     if (new_x_pos != []) and (new_y_pos != []):
  128.         player.update_sprite(
  129.             new_x_pos[animation_counter], new_y_pos[animation_counter])
  130.         # pygame.display.update()
  131.         # pygame.time.delay(10)
  132.  
  133.         animation_counter += 1
  134.         if animation_counter == fps:
  135.             animation_counter = 0
  136.             new_x_pos = []
  137.             new_y_pos = []
  138.  
  139.     world.fill(BLACK)
  140.  
  141.     player_list.draw(world)  # refresh player position
  142.     pygame.display.flip()
  143.     clock.tick(fps)
Advertisement
Add Comment
Please, Sign In to add comment