Last active
July 24, 2024 11:20
-
-
Save katzefudder/22c043bdcee54f707fc3faafe8b7b11e to your computer and use it in GitHub Desktop.
find player's names on their jersey using aws rekognition
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
import boto3 | |
# Define the team's roster | |
TEAM_ROSTER = [ | |
{"code": "bn72", "number": 72, "name": "Niklas Lunemann", "team": "EC Bad Nauheim"}, | |
{"code": "bn30", "number": 30, "name": "Maximilian Meier", "team": "EC Bad Nauheim"}, | |
{"code": "bn3", "number": 3, "name": "Nick Aichinger", "team": "EC Bad Nauheim"}, | |
{"code": "bn27", "number": 27, "name": "Alexander Dersch", "team": "EC Bad Nauheim"}, | |
{"code": "bn23", "number": 23, "name": "Marius Erk", "team": "EC Bad Nauheim"}, | |
{"code": "bn58", "number": 58, "name": "Christopher Fischer", "team": "EC Bad Nauheim"}, | |
{"code": "bn29", "number": 29, "name": "Leo Hafenrichter", "team": "EC Bad Nauheim"}, | |
{"code": "bn2", "number": 2, "name": "Paul Reiner", "team": "EC Bad Nauheim"}, | |
{"code": "bn4", "number": 4, "name": "Kevin Schmidt", "team": "EC Bad Nauheim"}, | |
{"code": "bn20", "number": 20, "name": "Patrick Seifert", "team": "EC Bad Nauheim"}, | |
{"code": "bn21", "number": 21, "name": "Eric Stephan", "team": "EC Bad Nauheim"}, | |
{"code": "bn6", "number": 6, "name": "Tim Coffman", "team": "EC Bad Nauheim"}, | |
{"code": "bn91", "number": 91, "name": "Marc El-Sayed", "team": "EC Bad Nauheim"}, | |
{"code": "bn7", "number": 7, "name": "Max Gerlach", "team": "EC Bad Nauheim"}, | |
{"code": "bn25", "number": 25, "name": "Fabian Herrmann", "team": "EC Bad Nauheim"}, | |
{"code": "bn61", "number": 61, "name": "Jordan Hickmott", "team": "EC Bad Nauheim"}, | |
{"code": "bn16", "number": 16, "name": "Christoph Körner", "team": "EC Bad Nauheim"}, | |
{"code": "bn93", "number": 93, "name": "Julian Lautenschlager", "team": "EC Bad Nauheim"}, | |
{"code": "bn82", "number": 82, "name": "Markus Lillich", "team": "EC Bad Nauheim"}, | |
{"code": "bn78", "number": 78, "name": "Kevin Orendorz", "team": "EC Bad Nauheim"}, | |
{"code": "bn36", "number": 36, "name": "Jerry Pollastrone", "team": "EC Bad Nauheim"}, | |
{"code": "bn9", "number": 9, "name": "Brent Raedeke", "team": "EC Bad Nauheim"}, | |
{"code": "bn34", "number": 34, "name": "Pascal Steck", "team": "EC Bad Nauheim"}, | |
{"code": "bn12", "number": 12, "name": "Cody Sylvester", "team": "EC Bad Nauheim"}, | |
{"code": "bn19", "number": 19, "name": "Taylor Vause", "team": "EC Bad Nauheim"}, | |
{"code": "bn100", "number": None, "name": "Harry Lange", "role": "Trainer", "team": "EC Bad Nauheim"}, | |
{"code": "bn101", "number": None, "name": "Adam Mitchell", "role": "Co-Trainer", "team": "EC Bad Nauheim"} | |
] | |
def detect_text(photo): | |
""" | |
Method to detect text in the given image. | |
Parameters: | |
photo (str): Path of the image. | |
Returns: | |
list: List of detected text lines with confidence greater than 90%. | |
""" | |
detected_text = [] | |
# Initialize boto3 client | |
client = boto3.client('rekognition') | |
try: | |
# Read image file | |
with open(photo, 'rb') as image: | |
response = client.detect_text(Image={'Bytes': image.read()}) | |
# Response from AWS Rekognition | |
text_detections = response['TextDetections'] | |
for text in text_detections: | |
if text['Type'] == 'LINE' and text['Confidence'] > 90.0: | |
detected_text.append(text['DetectedText']) | |
return detected_text | |
except FileNotFoundError: | |
print(f"Error: The file {photo} was not found.") | |
return [] | |
except boto3.exceptions.Boto3Error as e: | |
print(f"Error: {e}") | |
return [] | |
def match_player_names(detected_text): | |
""" | |
Method to match detected text against a team roster. | |
Parameters: | |
detected_text (list): List of detected text lines. | |
Returns: | |
list: List of matched player names with details. | |
""" | |
matched_players = [] | |
for text in detected_text: | |
for player in TEAM_ROSTER: | |
surname = player["name"].split()[-1].lower() | |
if text.lower() == surname: | |
matched_players.append(player) | |
return matched_players | |
if __name__ == "__main__": | |
photo_path = 'demo.jpg' | |
detected_text = detect_text(photo_path) | |
matched_players = match_player_names(detected_text) | |
print(f"Detected text: {detected_text}") | |
print("Matched player names:") | |
for player in matched_players: | |
print(f"- Code: {player['code']}, Number: {player['number']}, Name: {player['name']}, Team: {player['team']}") |
Author
katzefudder
commented
Jul 24, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment