陳昱翔
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Face_recognition Attendence List 臉部辨識點名系統 ###### tags: `Side projects` ## **序** 在某一次的練習賽中,我來到中科機器人自造基地。 我看到了門口有一個很特別的裝置,他們架了攝影機還有螢幕,當員工上班時就站在前面,臉部辨識打卡,瞬間科技感爆表。所以想說趁這個機會,自己做出差不多的程式,順便練習python專案 ![](https://i.imgur.com/aId3y5o.jpg) 圖為中科機器人自造基地 ## 建置環境 我認為建置環境是做專案最難的地方,你會遇到很多很鳥的問題,版本不符,硬體不支持等等。 花在弄環境的時間遠大於寫程式Debug的時間。 ## ANACONDA 學習 建置環境 `conda create --name FaceRecgnition python=3.6` 注意只能安 3.6 ![](https://i.imgur.com/q3coFSp.png) 檢查是否 以建置環境 ![](https://i.imgur.com/SjETMQf.png) 啟動環境 ![](https://i.imgur.com/Sf929Xo.png) 安裝Dlib(Face_recgnition要用的library) 首先要到此網站下載檔案[(dlib-19.8.1-cp36-cp36m-win_amd64.whl)](https://pypi.org/simple/dlib/) `pip install dlib-19.8.1-cp36-cp36m-win_amd64.whl` 安裝 opencv (包含 main & contrib module) `pip install opencv-contrib-python` 安裝 face_recognition `pip install face_recognition` 打開Anaconda Navigator ![](https://i.imgur.com/gNg2p1e.png) 更改已安裝好環境 ![](https://i.imgur.com/D02Q3q9.png) 打開vscode ![](https://i.imgur.com/mFlKqj0.png) 測試成功! ![](https://i.imgur.com/JINLBtt.png) ### 擷取圖像 ``` python= import cv2 cam=cv2.VideoCapture(1) while(True): ret,frame=cam.read() cv2.imshow("video",frame) if cv2.waitKey(1) & 0xFF == ord('q'): break if cv2.waitKey(1) & 0xFF == ord('x'): cv2.imwrite('pic1.jpg',frame) pic1=cv2.imread("pic1.jpg",0) cv2.imshow("picture",pic1) cam.release() cv2.destroyAllWindows() ``` ![](https://i.imgur.com/WTV41ry.gif) ### 建立sqldatabase ### 多線執行 [莫凡教學](https://www.youtube.com/watch?v=EeoFahm8FOE&list=PLXO45tsB95cKaHtKLn-jat8SOGndS3MEt&index=2) #### 可以同時運行畫面及輸入輸出 ``` python= import cv2 #import sql_lite_demo import threading def thread_job(): cam=cv2.VideoCapture(1) while(True): ret,frame=cam.read() cv2.imshow("video",frame) if cv2.waitKey(1) & 0xFF == ord('q'): break if cv2.waitKey(1) & 0xFF == ord('x'): cv2.imwrite('pic1.jpg',frame) pic1=cv2.imread("pic1.jpg",0) cv2.imshow("picture",pic1) cam.release() cv2.destroyAllWindows() def main(): added_thread=threading.Thread(target=thread_job) added_thread.start() strcmd=input("輸入指令") print('你輸入的指令為:'+strcmd) if __name__ == '__main__': main() ``` ### 不同檔案程式間呼叫變數 [教學網站](https://medium.com/@kweisamx0322/python-%E6%80%8E%E9%BA%BC%E5%9C%A8%E5%85%A9%E5%80%8B%E6%AA%94%E6%A1%88%E4%B9%8B%E9%96%93%E5%88%86%E4%BA%AB%E5%85%A8%E5%9F%9F%E8%AE%8A%E6%95%B8-8fced72f3550) 以下這三個程式可以輸入姓名及生日 然後另存![](https://i.imgur.com/ZEZMjeo.gif) 照片 ```python= #capture_picture.py import cv2 import threading import cmd import globals def video_show(): cam=cv2.VideoCapture(1) while(True): ret,frame=cam.read() cv2.imshow("video",frame) if cv2.waitKey(1) & 0xFF == ord('q'): break #print(globals.Takepic) if globals.Takepic: print(globals.picName) cv2.imwrite(globals.picName+'.jpg',frame) pic=cv2.imread(globals.picName+'.jpg',1) cv2.imshow("picture",pic) globals.Takepic=False cam.release() cv2.destroyAllWindows() def main(): globals.initialize() added_thread=threading.Thread(target=video_show) added_thread.start() strcmd=input("輸入指令: ") cmd.run_cmd(strcmd) if __name__ == '__main__': main() ``` ```python= # cmd.py import globals def run_cmd(str_cmd): if(str_cmd=='add'): a=input("請輸入英文名字") b=input("請輸入生日") globals.picName=a globals.Takepic=True else: print(str_cmd+' is not command') ``` ```python= #globals.py def initialize(): global Takepic,picName Takepic=False picName='null' ``` ### 建立資料庫 這次我第一次使用Python的database,所以我速刷了這一部影片 https://www.youtube.com/watch?v=pd-0G0MigUA 首先我需要把已輸入的資料存入datebase中 ```python= def save_file_to_database(): conn =sqlite3.connect('personal_file.db') c=conn.cursor() c.execute("INSERT INTO personal_file VALUES(?,?)",(globals.picName,globals.birthday)) conn.commit() conn.close() ``` 然後在剛影像辨識處呼叫檔案中全部的資料 [參考資料1](https://stackoverflow.com/questions/52815376/how-to-fetch-data-from-sqlite-using-python) [參考資料2](https://nkust.gitbook.io/python/sqlite-liao-cao-zuo-jie) ```python= c.execute("SELECT * FROM personal_file ") rows = c.fetchall() for row in rows: print(row[0])//拿第一個資料 ``` 我在製作出完整的函式 ```python= def take_data(): conn =sqlite3.connect('personal_file.db') c=conn.cursor() c.execute("SELECT * FROM personal_file ") rows = c.fetchall() for row in rows: print(row[0]) conn.close() ``` ![](https://i.imgur.com/SQxmqus.png) 測試結果成功。 最後把這函式放在儲存完照片後,執行。 ## 開始處理臉部辨識 首先我們先分析範例程式 [範例程式](https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py) ```python= # Load a sample picture and learn how to recognize it. obama_image = face_recognition.load_image_file("obama.jpg") obama_face_encoding = face_recognition.face_encodings(obama_image)[0] # Load a second sample picture and learn how to recognize it. biden_image = face_recognition.load_image_file("biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_image)[0] # Create arrays of known face encodings and their names known_face_encodings = [ obama_face_encoding, biden_face_encoding ] known_face_names = [ "Barack Obama", "Joe Biden" ] ``` 可以看到它需要做三個步驟地處裡,首先先讀入照片,再將照片經過編碼,最後處存以編碼過的照片。此處我們看看能不能使用提取資料庫的方法尋找照片信息。 #### 實驗看看如何在array中增加新元素 https://www.runoob.com/python/att-list-append.html ```python= arr=['Ben','John','Belly'] arr.insert(len(arr),'Benson') #讀取陣列大小後在最後面加上新元素 #或是用 arr.append('Benson') 後來才發現的 print(arr,len(arr)) ``` 輸出結果 ![](https://i.imgur.com/nue5Qen.png) #### 從資料庫呼叫照片信息 預想結果為把兩個陣列丟進去函式裡,函式會把資料庫的值放進陣列之中。 ```python= def take_data_from_database(file_name,file_list_image_encoding,): conn =sqlite3.connect('personal_file.db') c=conn.cursor() c.execute("SELECT * FROM personal_file ") rows = c.fetchall() for row in rows: file_name.append(row[0]) temp_image_read=face_recognition.load_image_file('Database/picture/'+row[0]+'.jpg') file_list_image_encoding.append(face_recognition.face_encodings(temp_image_read)[0]) conn.close() ``` #### 測試資料結果 使用到此函式處 ![](https://i.imgur.com/oanuGmM.png) 執行範例程式。 ![](https://i.imgur.com/7rmV8g8.gif) ### 影像跑起來卡卡的 上網搜尋結果後發現我好像沒安裝好dlib ![](https://i.imgur.com/xqyUpS6.png) 重點: 如果想要影像順一點的話,可以Resizing(我已經做過了,但是太小的話,沒辦法辨識出臉部),或者是直接打`import dlib,print(dlib.DLIB_USE_CUDA)`,如果沒有成功輸出的話,代表沒有安裝好dlib(我安裝失敗) ![](https://i.imgur.com/5lXE6W4.png) 現階段可能難以解決此問題,因為Window版本cmake很難用。 #### 臉部辨識主要程式 ```python= #******************************************************************************************************** small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) #縮小原圖 幫助運算更快速 face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(known_face_encodings, face_encoding,0.6) name = "Unknown" if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] face_names.append(name) #放上文字 for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) #******************************************************************************************************** ``` https://blog.gtwang.org/programming/python-iterate-through-multiple-lists-in-parallel/ https://blog.csdn.net/u014291497/article/details/51059686 ### 設計UI介面 第一次用Tk

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully