程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

HTML embedded python code (python does face recognition)

編輯:Python

大家好,又見面了,我是你們的朋友全棧君.

最近閒來無事,Research Research runs on AndroidPython.

Reminds me of playing beforekivy技術,kivy[1]是一個跨平台的UI框架.Of course the most useful to us is,kivy可以把pythonThe code is packaged into AndroidApp.But since the toolchain packaged by Android is very long,包括android sdk打包java代碼、ndk編譯python、 編譯各種python依賴包,Often it takes the whole day to go from getting started to giving up.This time, I used a serious research mentality,終於找到一個解決方案,於是有了這篇文章:

•只要會pythonCan write AndroidApp,No Android development foundation required,無需編譯•There is also interactive on mobilepython解釋器,直接調試python代碼•可以使用各種python庫,包括numpy/opencv等機器學習包•Can interact with the Android interface,Use phone hardware,比如攝像頭

Then we use face recognitionApp為例,See how to do it in a few simple steps.Take a look at the finished product first:

第一步:安裝airport.apk

AirPortIt is an Android that I have compiledApp,裡面包含了pythoninterpreter and some commonly used onespython庫.

第二步:連接手機的python解釋器

啟動手機上的AirPort應用,就會運行python解釋器.I have one built inssh服務器,Very handy for debugging code.The phone's will be displayed when the app startsip地址.

在電腦上使用ssh命令,to connect to the phone.

注意:Make sure your phone and computer are in the same local area network.

#在電腦上連接手機,注意這裡ip需要替換成AirPort顯示的ipssh -p 8000 [email protected]#輸入密碼meteorix

Then you can enjoy it on your phonepython了,比如試試

>>>import os>>>os.getcwd()’/data/data/org.airtest.airport/files/app’>>>import requests>>>r = requests.get(“https://www.baidu.com”)>>>r.status_code200

第三步: a cameraApp

在kivy的官方文檔中,We can find such a cameraexample[2]

代碼非常簡單,Builder.load_stringThe function loads a piece of configuration,這是kivy提供的UI定義語言kivy language.點擊UI上創建的Capture按鈕,回調CameraClick.capture()函數,用python實現函數功能.

from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.boxlayout import BoxLayoutimport timeBuilder.load_string(”’:orientation: ‘vertical’Camera:id: cameraresolution: (640, 480)play: FalseToggleButton:text: ‘Play’on_press: camera.play = not camera.playsize_hint_y: Noneheight: ’48dp’Button:text: ‘Capture’size_hint_y: Noneheight: ’48dp’on_press: root.capture()”’)class CameraClick(BoxLayout):def capture(self):”’Function to capture the images and give them the namesaccording to their captured time and date.”’camera = self.ids[‘camera’]timestr = time.strftime(“%Y%m%d_%H%M%S”)camera.export_to_png(“IMG_{}.png”.format(timestr))print(“Captured”)class TestCamera(App):def build(self):return CameraClick()TestCamera().run()

將這段代碼保存為kvmain.py文件,We can run it directly on the computer.If your computer has a camera,You can see the cameraApp的效果.

第四步:Push the code to the Android phone

This step needs to be done,put this cameraAppPush to Android phone,然後啟動AirPort應用.

If you know a little bit about Android phones,你應該用過adb工具.The principle here is to useadb連接手機,將kvmain.py推送到手機/sdcard/kv/kvmain.py路徑.然後啟動AirPort應用,will be loaded in this pathpython代碼.

1.可以從這裡[3]下載對應操作系統的adb工具.2.用usb線將手機連接到電腦,打開手機的開發者選項/usb調試開關,然後檢查adb連接.看到device就說明連接正常,如果是其他狀態,Need to check the phone configuration a little bit.

adb devices # 查看adb連接List of devices attachedABCDEFGHIJK device

3.將kvmain.py推送到手機/sdcard/kv/kvmain.py路徑

adb shell mkdir -p /sdcard/kvadb push kvmain.py /sdcard/kv/kvmain.py

If you don't know what the above is saying,可以先google/baidu一下adb使用教程.

Restart on the phoneAirPort應用,to see our cameraApprun on the phone.

第五步:Add face recognition function

這一步,我們主要用到了opencv的人臉識別接口,詳細原理參考opencv tutorial[4]

對我們來說,Only the following simple code is used here

import cv2detector = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)img = cv2.imread(‘faces.jpg’)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = detector.detectMultiScale(gray, 1.3, 5)print(faces)

Then make a change to our cameraApp:

1.Read the picture from the camera,調用opencv人臉識別接口2.The coordinates of the face that will be recognized,Draw it to the corresponding position on the phone screen

bbox = BoundingBox(name=face_name, size_hint=(None, None))…for loc in faces:# calculate position of the facex, y, w, h = loct = int(anchor_t – y*sh)b = int(anchor_t – (y+h)*sh)r = int(anchor_l + x*sw)l = int(anchor_l + (x+w)*sw)# update bounding boxbbox.pos = (int(l), int(b))bbox.size = (int(r-l), int(t-b))…

當然,We also need to do some debugging for Android phones.I put the final code heregithub倉庫airface[5]https://github.com/Meteorix/airface

再次,We push the code to the phone

adb push src/* /sdcard/kv/

Restart the application and you can see what was shown at the beginning of the articleGIF效果了.

What’s Next?

PythonCould have done a lot of fun things,現在pythonDevelopers can also write directly to AndroidApp啦.

Let's open our minds a bit:

•Train a classifier that recognizes all the faces in your house,Even a cat face(對,opencvBuilt-in cat face recognition)•用pythonWrite a voice assistantapp,Personally customized Xiao Ai classmates

bigger brain:

•在手機上用python跑TensorFlow?•Write a real mobile phone WeChat robot?

Currently I am using this way,Android for writing artificial intelligencedemo.You are also welcome to try itpython寫安卓app,探索更多可能性,有問題留言交流.

References

[1] kivy: https://github.com/kivy/kivy[2] 攝像頭的example: https://kivy.org/doc/stable/examples/gen__camera__main__py.html[3] adb下載: https://github.com/AirtestProject/Airtest/tree/master/airtest/core/android/static/adb[4] opencv tutorial: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html[5] 源碼倉庫: https://github.com/Meteorix/airface

發布者:全棧程序員棧長,轉載請注明出處:https://javaforall.cn/128338.html原文鏈接:https://javaforall.cn


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved