Skip to content

Commit a1f51cd

Browse files
authored
Update python script
1 parent c1535c4 commit a1f51cd

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

labs/PiDay-2022-Vision/pythonscript/helmetdetection.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
compartment_id = "<compartment id>"
2121
input_prefix = "<folder name for images>"
2222
output_prefix = "<output folder name for results>"
23+
max_results_per_image = 25
2324

2425
# Auth Config Definition
2526
config = oci.config.from_file('~/.oci/config')
@@ -49,6 +50,7 @@
4950
### Vision AI
5051
# Send the Request to Service with Multiple Features
5152
image_object_detection_feature = ImageObjectDetectionFeature()
53+
image_object_detection_feature.max_results = max_results_per_image
5254
features = [image_object_detection_feature]
5355

5456
# Setup Input Location
@@ -76,9 +78,9 @@
7678
final_prefix= output_prefix+"/"+res.data.id+"/"+namespace_name+"_"+bucket_name+"_"+input_prefix+"/"
7779

7880
# Logic to perform the following statistics:
79-
# 1. Count the number of persons and hardhats in each image, and total up the counts
80-
# 2. Report the person and hardhat counts, and the number of images processed
81-
# 3. Also, list the names of images where the person count and hardhat count don’t match
81+
# 1. Count the number of persons and helmets in each image, and total up the counts
82+
# 2. Report the person and helmets counts, and the number of images processed
83+
# 3. Also, list the names of images where the person count and helmets count don’t match
8284

8385
# Wait for images to be processed
8486
print("Wait for images to be analyzed. Timeout after 90 seconds")
@@ -96,12 +98,14 @@
9698
time.sleep(5)
9799
i+= 1
98100

99-
person_count=0
100-
hat_count=0
101-
total_person_count=0
102-
total_hat_count=0
103-
image_counter=0
104-
no_match_list=[]
101+
print("\n")
102+
103+
person_count = 0
104+
helmet_count = 0
105+
total_person_count = 0
106+
total_helmet_count = 0
107+
image_counter = 0
108+
no_match_list = []
105109

106110
# List all JSON responses received by Vision AI
107111
object_storage_client = ObjectStorageClient(config)
@@ -111,29 +115,30 @@
111115
prefix = final_prefix
112116
)
113117

114-
# Count number of persons and number of hats
118+
# Count number of persons and number of helmets
115119
for i in object_list.data.objects:
120+
person_count = 0
121+
helmet_count = 0
122+
116123
image_counter=image_counter+1
117124
body=object_storage_client.get_object(namespace_name, bucket_name, object_name=i.name)
118125
dict_test= json.loads(body.data.content.decode('utf-8'))
119126
for j in dict_test['imageObjects']:
120-
if (j['name'] =='Person' or j['name']=='Man' or j['name']=='Woman' or j['name']=='Human'):
121-
person_count = person_count+1
127+
if (j['name'] == 'Person' or j['name'] == 'Man' or j['name'] == 'Woman' or j['name'] == 'Human'):
128+
person_count = person_count + 1
122129
if (j['name'] == 'Helmet'):
123-
hat_count = hat_count+1
130+
helmet_count = helmet_count + 1
124131

125-
if (person_count != hat_count):
132+
if (person_count != helmet_count):
126133
no_match_list.append(i.name)
127134

128135
total_person_count = total_person_count + person_count
129-
total_hat_count = total_hat_count + hat_count
130-
person_count=0
131-
hat_count=0
136+
total_helmet_count = total_helmet_count + helmet_count
132137

133-
print ("Number of persons found in images:", person_count,"\n")
134-
print ("Number of hardhats found in images:", hat_count, "\n")
138+
print ("Number of persons found in images:", total_person_count,"\n")
139+
print ("Number of helmets found in images:", total_helmet_count, "\n")
135140
print ("Number of images processed:", image_counter, "\n")
136-
print ("Names of images where hat count is not equal to total number of persons:\n")
141+
print ("Names of images where at least one person is not wearing their helmet:\n")
137142

138143
for i in no_match_list:
139144
i=re.sub(final_prefix,'',i)

0 commit comments

Comments
 (0)