def label_rekognition(self, event = None):
client=boto3.client('rekognition')
img = cv2.imread(self.imageFile)
imOut = img
img_PIL = Image.open(self.imageFile)
width, height = img_PIL.size
with open(self.imageFile, 'rb') as image:
label_response = client.detect_labels(
Image={'Bytes': image.read()})
label_jsonfile = "labeloutput.json"
with open(label_jsonfile, 'w') as fp:
json.dump(label_response, fp)
for label in label_response['Labels']:
for boundingbox in label['Instances']:
cv2.rectangle(imOut,
(int(width*boundingbox['BoundingBox']['Left']),
int(height*boundingbox['BoundingBox']['Top'])),
(int(width*(boundingbox['BoundingBox']['Left']+
boundingbox['BoundingBox']['Width'])),
int(height*(boundingbox['BoundingBox']['Top']+
boundingbox['BoundingBox']['Height']))),
self.color_1,
int(self.linesizespinbox.get()))
cv2.putText(imOut,
label['Name'],
(int(width*boundingbox['BoundingBox']['Left']),
int(height*boundingbox['BoundingBox']['Top'])),
self.fontcv2Var.get(),
int(self.fontsizespinbox.get()),
self.color_2,
int(self.linesizespinbox.get()),
self.fontlinetypecv2Var.get())
while True:
cv2.imshow("Output", imOut)
k = cv2.waitKey(0) & 0xFF
# 若按下 q 鍵,則離開
if k == 113:
break
cv2.destroyAllWindows()