I execute python code for an object-recognition task and it works very well but when I implement it into an android application using Chaquopy it dosn't work. This is because the first insruction in the python code aims to open the camera using this insruction cap = cv2.videoCapture(url) and this instruction dosn't work on the android device.
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textview);
if(!Python.isStarted())
Python.start(new AndroidPlatform(this));
Python py = Python.getInstance();
PyObject pyobj = py.getModule("main");
textView.setText(pyobj.callAttr("main").toString());
}
}
main.py
import cv2
import numpy as np
def main():
thres = 0.50
nms_threshold = 0.20
cap = cv2.VideoCapture('http://192.168.137.10:8081/')
classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames=[line.rstrip() for line in f]
#print(classNames)
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(300,300)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
success,img = cap.read()
classIds, confs, bbox = net.detect(img,confThreshold=thres)
bbox = list(bbox)
confs = list(np.array(confs).reshape(1, -1)[0])
confs = list(map(float, confs))
#print(type(confs[0]))
#print(confs)
indices = cv2.dnn.NMSBoxes(bbox,confs,thres,nms_threshold)
#print(indices)
for i in indices:
i=i[0]
return (classNames[classIds[i][0] - 1])
Logcat
2022-06-07 22:19:43.083 26043-26043/com.example.thirdeye E/cv::error(): OpenCV(4.5.1) Error: Bad argument (CAP_IMAGES: can't find starting number (in the name of file): http://192.168.137.10:8081/) in icvExtractPattern, file /home/smith/git/chaquo/python/server/pypi/packages/opencv-contrib-python/build/4.5.1.48/cp38-cp38-android_16_armeabi_v7a/src/opencv/modules/videoio/src/cap_images.cpp, line 253
2022-06-07 22:19:43.088 26043-26043/com.example.thirdeye E/OpenCV/4.5.1: [ERROR:0] global /home/smith/git/chaquo/python/server/pypi/packages/opencv-contrib-python/build/4.5.1.48/cp38-cp38-android_16_armeabi_v7a/src/opencv/modules/videoio/src/cap.cpp (142) open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.1) /home/smith/git/chaquo/python/server/pypi/packages/opencv-contrib-python/build/4.5.1.48/cp38-cp38-android_16_armeabi_v7a/src/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): http://192.168.137.10:8081/ in function 'icvExtractPattern'
2022-06-07 22:19:43.089 26043-26043/com.example.thirdeye A/libc: Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xdc40d601 in tid 26043 (xample.thirdeye), pid 26043 (xample.thirdeye)