iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 17

[Python][OpenCV][AWS] Rekognition(2)celibrities

偵測知名人士
https://ithelp.ithome.com.tw/upload/images/20201002/20119608Ga1TqW7KQD.jpg

def celebrities(self, event = None):
    self.Name = ""
    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:
        celebrities_response = client.recognize_celebrities(
                                       Image=
                                       {'Bytes': image.read()})
    cele_jsonfile = "celebritiesoutput.json"
    with open(cele_jsonfile, 'w') as fp:
        json.dump(celebrities_response, fp)
    for celebrity in celebrities_response['CelebrityFaces']:
        for eachperson in celebrity['Face']['Landmarks']:
            cv2.circle(imOut,
                       (int(width*eachperson['X']),
                        int(height*eachperson['Y'])),
                        1, self.color_1, -1)
        cv2.rectangle(imOut,
                      (int(width*celebrity['Face']['BoundingBox']['Left']),
                       int(height*celebrity['Face']['BoundingBox']['Top'])),
                      (int(width*(celebrity['Face']['BoundingBox']['Left']+
                       celebrity['Face']['BoundingBox']['Width'])),
                      int(height*(celebrity['Face']['BoundingBox']['Top']+
                       celebrity['Face']['BoundingBox']['Height']))),
                      self.color_2,int(self.linesizespinbox.get()))
        self.DisplaySceneMarkInfo.insert(tk.END,celebrity['Urls'])
        self.DisplaySceneMarkInfo.insert(tk.END,celebrity['Name'])
        self.Name = str(celebrity['Name'])
        self.DisplaySceneMarkInfo.insert(tk.END,"\n")
    self.open_webbrowser()

    while True:
        # 複製一份原始影像
        #imOut = img.copy()
        # 顯示結果
        cv2.imshow("Output", imOut)
        # 讀取使用者所按下的鍵
        k = cv2.waitKey(0) & 0xFF
        # 若按下 q 鍵,則離開
        if k == 113:
            break
    # 關閉圖形顯示視窗
    cv2.destroyAllWindows()

打開瀏覽器搜尋

def open_webbrowser(self):
    import webbrowser
    from urllib.parse import quote
    new = 2 # not really necessary, may be default on most modern browsers
    base_url = "http://www.google.com/?#q="
    #query = input("Please enter your search query: ")
    final_url = base_url + quote(self.Name)
    webbrowser.open(final_url, new=new)
    

https://ithelp.ithome.com.tw/upload/images/20201002/20119608Lbyl3t525R.jpg


上一篇
[Python][OpenCV][AWS]Rekognition(1)face_rekognition
下一篇
[Python][OpenCV][AWS] Rekognition(3)label_rekognition
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言