-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognize.py
More file actions
120 lines (105 loc) · 4.02 KB
/
Copy pathrecognize.py
File metadata and controls
120 lines (105 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
FINDING PRESIDENT BIDEN
Author: Jim Duran
Date: 2021-03
Joe Biden's Celebrity ID is 3Zw7fr
This script expects to see a directory of frames, one per minute, of a channel feed.
It takes each frame and uses facial recognition to find celebrities.
If the celebrity is Joe Biden, ID 3Zw7fr, it highlights the frame.
"""
import boto3
import os
import datetime as dt
import shutil
from time import sleep, strftime
from os import path
s3 = boto3.resource('s3')
#Input folder will include 1440 frames, one frame per minute
sourceFolder = '../../../../../../Volumes/VTNA_Second_Stream/CNN/frames/'
outputFolder = 'output/'
celebrityID = '3Zw7fr' #Default is Biden's celebrity ID, 3Zw7fr
bidenFrames = [] #empty variable
def find_joe_biden(photo):
client=boto3.client('rekognition')
status = ''
area = 0
otherCelebs = []
data = []
frameDest = ''
try:
with open(photo, 'rb') as image:
response = client.recognize_celebrities(Image={'Bytes': image.read()})
except FileNotFoundError:
issueFrame = photo.replace('../../../../../../Volumes/VTNA_Second_Stream/CNN/frames/','')
print('Trouble finding a frame')
sleep(30)
data = ['error','error']
return data
for celebrity in response['CelebrityFaces']:
if celebrity['Id'] == '3Zw7fr': #Biden's celebrity ID
area = (float(celebrity['Face']['BoundingBox']['Width'])+float(celebrity['Face']['BoundingBox']['Height']))/2
"""
The area is the size of the celebrity's face on the screen as a ratio of total screen size.
0.06 is the size of a little thumbnail in the corner
0.15 or greater is a head and shoulder shot
"""
if area >= 0.12:
status = ">>>>>>------>>>>>> PRESIDENT BIDEN <<<<<<-------<<<<<<"
frameDest = photo.replace(sourceFolder,'')
shutil.copy2(photo,outputFolder+frameDest)
else:
otherCelebs.append(celebrity['Name'])
data.append(status)
fullCelebList = ";".join(otherCelebs)
data.append(fullCelebList)
return data
def reportWriter(data,date):
f = open('output/Biden_Report_'+str(date)+'.txt','w')
f.write('Title:BIDEN REPORT '+str(date)+'\n')
for item in data:
f.write(str(item[0]+item[1]+' '+item[2])+'\n')
f.close()
print("File Saved")
return
def timeIntrepreter(filename):
hour = int(filename[13:15])
minutes = int(filename[20:23])
addhours, min = divmod(minutes,60)
finalHour = hour + addhours
return "%d:%02d " % (finalHour,min)
def main(date):
ctr = 0
blocks = ['_0000img','_0400img','_0800img','_1200img','_1600img','_2000img']
bidenFrames = []
for block in blocks:
blockPrefix = 'CNN_' + date + block
frameList = []
i = 1
while i <= 250: #2 for testing, 250 for production
tempFileName = blockPrefix+str(i).zfill(3)+'.jpg'
print(tempFileName)
frameList.append(tempFileName)
i += 1
for photo in frameList:
if '.jpg' in photo:
print(photo)
timestamp = timeIntrepreter(photo)
if path.exists(sourceFolder + photo):
photoReport = find_joe_biden(sourceFolder + photo)
bidenFrames.append([timestamp,photoReport[0],photoReport[1]])
if photoReport[0] == ">>>>>>------>>>>>> PRESIDENT BIDEN <<<<<<-------<<<<<<":
ctr += 1
else:
photoReport = "missing frame"
bidenFrames.append([timestamp,photoReport,'none'])
print('does not exist')
continue
print("finished "+block+" Length of frames:"+str(len(bidenFrames)))
print(bidenFrames)
print("Biden Count:"+str(ctr))
return bidenFrames
if __name__ == "__main__":
print("running main program")
date = input('select a date YYYYMMDD:')
report = main(date)
reportWriter(report,date)