[Python] Real-time object detection with iPad camera

Recent smartphones are a mass of sensors such as cameras, acceleration / angular velocity sensors, and barometric pressure sensors. By combining this with python, it is possible to easily observe the state in the real world. In this article, I will explain how to make python recognize what the object seen from the iPad camera is.

What you need is as follows.

** ① iOS device ** iPad or iPhone. The iPad has a larger screen and is easier to operate.

** ② App "Pyto" for running python in iOS environment ** It is convenient because the module (Open CV) used for image analysis is installed in the default state. The fee is 1220 yen (as of November 2020), but the following script can be executed even during the 3-day trial period, so please try it.

Pyto App Store Link

1. 1. Install Pyto

Install "Pyto -Python 3" from the app store. You can choose to pay or try it for 3 days only. There is no problem even if you try it.

https://apps.apple.com/jp/app/pyto-python-3/id1436650069

environment

Device: iPad Air 3
OS:iPad OS 14.1
Pyto:14.1.1

2. File creation

The sample code is as follows. OpenCV is a library that implements the functions required to process images and videos. Use this to detect what the object on the iPad camera is.

objectDitection.py



import cv2
from cv2 import dnn
import numpy as np
import time
import os

inWidth = 224
inHeight = 224
WHRatio = inWidth / float(inHeight)
inScaleFactor = 0.017
meanVal = (103.94, 116.78, 123.68)
prevFrameTime = None
currentFrameTime = None
device = 0

if __name__ == "__main__":
   modelfolder = "./MobileNet-Caffe-master"
   net = dnn.readNetFromCaffe(
       os.path.join(modelfolder, "mobilenet_v2_deploy.prototxt"),
       os.path.join(modelfolder, "mobilenet_v2.caffemodel"),
   )
   cap = cv2.VideoCapture(device)
   f = open(os.path.join(modelfolder, "synset.txt"), "r")
   classNames = f.readlines()
   showPreview = True

   while cap.isOpened():
       # capture frame-by-frame
       ret, frame = cap.read()
       # check if frame is not empty
       if not ret:
           # print("continue")
           continue
       frame = cv2.autorotate(frame, device)
       rgbFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
       blob = dnn.blobFromImage(rgbFrame, inScaleFactor, (inWidth, inHeight), meanVal)
       net.setInput(blob)
       detections = net.forward()

       maxClassId = 0
       maxClassPoint = 0
       for i in range(detections.shape[1]):
           classPoint = detections[0, i, 0, 0]
           if classPoint > maxClassPoint:
               maxClassId = i
               maxClassPoint = classPoint

       className = classNames[maxClassId]
       # print("class id: ", maxClassId)
       # print("class point: ", maxClassPoint)
       # print("name: ", className)
       prevFrameTime = currentFrameTime
       currentFrameTime = time.time()
       if prevFrameTime != None:
           i = 1
           # print(1.0 / (currentFrameTime - prevFrameTime), "fps")

       if showPreview:
           font = cv2.FONT_HERSHEY_SIMPLEX
           size = 1
           color = (255, 255, 255)
           weight = 2
           cv2.putText(frame, className, (10, 30), font, size, color, weight)
           cv2.putText(frame, str(maxClassPoint), (10, 60), font, size, color, weight)
           cv2.imshow("detections", frame)

3. 3. Download the learned file for object detection to your iPad

This time, we will use the following object detection library. From https://github.com/shicai/MobileNet-Caffe MobileNet-Caffe-master download. Save this in the same directory as the executable file.

① Download the file from the above link on iOS (2) Extract the zip file (available on iOS 13 or later) ③ Move the file to the directory containing the executable file (default is icloud/pyto)

Four. Permission to the folder

From the settings in pyto, give access permission to "folder where pyto executable file exists" and "folder where MobileNet-Caffe-master exists" by the following procedure.

sample_1.jpg

sample_2.jpg

sample_3.jpg

Five. Run

The screen will be displayed on the console just by pressing the execute button on the upper right. You don't even need to build an environment or install modules, so you can really do it in 3 minutes! It also supports script execution calls from Siri as a function of Pyto.

Articles that I used as a reference https://github.com/shicai/MobileNet-Caffe https://qiita.com/yamamido0268/items/e1172d25072da1687c49 http://asukiaaa.blogspot.com/2018/03/opencvdnnpythonmobilenet.html

Recommended Posts

[Python] Real-time object detection with iPad camera
Real-time object detection Android app with TensorFlow and Camera X
Camera capture with Python + OpenCV
Blockchain tampering detection with Python
2020/02 Python 3.7 + TensorFlow 2.1 + Keras 2.3.1 + YOLOv3 Object detection with the latest version
Face detection with YOLO Face (Windows10, Python3.6)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Face recognition with camera with opencv3 + python2.7
Single pixel camera to experience with Python
Try Object detection with Raspberry Pi 4 + Coral
Cooking object detection with yolo + image classification
Hello World and face detection with OpenCV 4.3 + Python
FizzBuzz with Python3
Scraping with Python
Python built-in object
Statistics with python
Scraping with Python
Python with Go
I tried object detection using Python and OpenCV
Twilio with Python
Play with 2016-Python
Tested with Python
Python built-in object
with syntax (Python)
Let's try real-time object detection using Faster R-CNN
Bingo with python
Excel with Python
Microcomputer with Python
Cast with python
Allow real-time code checking in Python development with VS Code
The 14th offline real-time writing reference problem with Python
Face detection from images taken with Raspberry Pi camera
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Anomaly detection with Amazon Lookout for Vision Part 2 (Python3.6)
Display USB camera video with Python OpenCV with Raspberry Pi
[Summary] Object detection model "End-to-End Object Detection with Transformers" using Transformer
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI