Created
March 16, 2018 03:18
-
-
Save peterlyeung/a4cdcf9d556ea698d1b4a1094c5e8bbb to your computer and use it in GitHub Desktop.
Use CV to grab iPhone mirrored on screen to get screenshots and then send to Rekognition using the boto library to train engine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------------------------_ | |
# This script can be ran in 2 modes: | |
# python play.py train | |
# or | |
# python play.py Run | |
# | |
# Use the training mode to train the AI. | |
# Once the AI has been trained, then run it in the play mode. The matching screenshot | |
# from the learning mode will be displayed on screen. | |
#----------------------------------------------------------------------------- | |
import numpy as np | |
from PIL import ImageGrab | |
import cv2 | |
import time | |
import boto3 | |
import re | |
import sys | |
BUCKET_NAME = "somebucket" | |
mode = 'train' # modes can be train or run | |
NUM_IMAGES = 9 # Learning Games has 9 tiles | |
#----------------------------------------------------------------------------- | |
# Train: upload image to S3 | |
#----------------------------------------------------------------------------- | |
def screen_record(): | |
last_time = time.time() | |
fileNameCounter = 11 | |
counter = 1 | |
while(counter <= NUM_IMAGES): | |
img = ImageGrab.grab(bbox=(0,0,720,1280)) | |
imageName = str(counter)+'.png' | |
printscreen = np.array(img) | |
if (mode == 'train'): | |
img.save('./images/' + imageName) | |
s3 = boto3.client('s3') | |
s3.upload_file('./images/'+imageName, BUCKET_NAME, imageName, ExtraArgs={'ACL':'public-read'}) | |
#index | |
for record in index_faces(BUCKET_NAME, imageName, 'emp__photos',imageName): | |
face = record['Face'] | |
# details = record['FaceDetail'] | |
#print ("Face " + str(face['Confidence'])) | |
#print (" FaceId: " + face['FaceId']) | |
#print (" ImageId: " + face['ImageId']) | |
#print("Uploaded " + str(counter)+ ".png to S3") | |
elif (mode == 'run'): | |
# send image to recognition | |
img.save('./images/matchThis.png') | |
s3 = boto3.client('s3') | |
s3.upload_file('./images/matchThis.png', BUCKET_NAME, 'matchThis.png', ExtraArgs={'ACL':'public-read'}) | |
face_recog(printscreen) | |
print(str(counter) + ' - Processing took {} seconds'.format(time.time()-last_time)) | |
last_time = time.time() | |
#cv2.imshow('window',cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB)) | |
if cv2.waitKey(25) & 0xFF == ord('q'): | |
cv2.destroyAllWindows() | |
break | |
counter += 1 | |
fileNameCounter += 1 | |
#----------------------------------------------------------------------------- | |
# Index faces using Boto | |
#----------------------------------------------------------------------------- | |
def index_faces(bucket, key, collection_id, image_id): | |
rekognition = boto3.client("rekognition") | |
response = rekognition.index_faces( | |
Image={ | |
"S3Object": { | |
"Bucket": bucket, | |
"Name": key, | |
} | |
}, | |
CollectionId=collection_id, | |
ExternalImageId=image_id, | |
) | |
return response['FaceRecords'] | |
#----------------------------------------------------------------------------- | |
# Run: Ask rekognition to match the face | |
#----------------------------------------------------------------------------- | |
def face_recog(img): | |
#cv2.imwrite('./face_recog.jpg', img) | |
with open("./images/MatchThis.png", "rb") as imageFile: | |
f = imageFile.read() | |
buf = bytearray(f) | |
client = boto3.client('rekognition') | |
response = client.search_faces_by_image( | |
CollectionId='emp__photos', | |
Image={ | |
'Bytes': buf | |
} | |
#MaxFaces=1, | |
#FaceMatchThreshold=80 | |
) | |
#print (response) | |
if len(response['FaceMatches']) == 0: | |
print ("FaceMatches = 0") | |
return False | |
else: | |
res = response['FaceMatches'][0]['Face']['ExternalImageId'] | |
matchObj = re.match( r'(.*)_([0-9]+).jpg', res, re.M|re.I) | |
if matchObj: | |
print ("matchObj.group(1) " + matchObj.group(1)) | |
return matchObj.group(1) | |
else: | |
print ("Matched this image file that was trained: " + res) | |
img = cv2.imread('./images/'+res) | |
#cv2.imshow('window',cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | |
cv2.imshow('image',img) | |
return res | |
#------------------------------------------------------- | |
# Main | |
#------------------------------------------------------- | |
if __name__ == "__main__": | |
# print command line arguments | |
for arg in sys.argv[1:]: | |
if arg == 'train': | |
mode = 'train' | |
elif (arg == 'run'): | |
mode = 'run' | |
print ("Mode: "+ mode) | |
screen_record() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment