Chaquopyをつかい、Android(Kotlin)からPythonへCameraXから取得したAnalysis Imageを渡し、OpenCVで物体検出を行ったあと、結果をAndroidに返すプログラムを考えています。
大まかな流れとしては まずCamaraX Analysisから得たImageProxyをBitmapにコピーして、それをPNGに変換した後に、BytearrayでPythonに渡します。
Pythonで解析後、解析結果をPNGに変換、それをbase64でエンコードして文字列でAndroidに返します。
あとは、文字列をデコードしたものをbitmapに変換して描写といった流れです。
以下のコードです。
Pythonの画像解析の部分は未だ適当な処理しか入れていませんが、既に動作がカクカクの状態です。
動作はするものの、処理が遅いためかCameraXからの画像が数秒遅れで渡されてきます。
Bitmapを上手くPythonに受け渡しする方法があれば良いのですが、何か良い方法はないでしょうか。
よろしくお願いいたします。
Main_Activity.kt
1..................... 2............... 3......... 4 preview = Preview.Builder() 5 .setTargetAspectRatio(AspectRatio.RATIO_4_3) 6 .setTargetRotation(fragmentCameraBinding.viewFinder.display.rotation) 7 .build() 8 9 imageAnalyzer = ImageAnalysis.Builder() 10 .setTargetAspectRatio(AspectRatio.RATIO_4_3) 11 .setTargetRotation(fragmentCameraBinding.viewFinder.display.rotation) 12 .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) 13 .setOutputImageFormat(OUTPUT_IMAGE_FORMAT_RGBA_8888) 14 .build() 15 .also { 16 it.setAnalyzer(cameraExecutor) { image -> 17 if (!::bitmapBuffer.isInitialized) { 18 bitmapBuffer = Bitmap.createBitmap( 19 image.width, 20 image.height, 21 Bitmap.Config.ARGB_8888 22 ) 23 } 24 detectObjects(image) 25 } 26 } 27 cameraProvider.unbindAll() 28 try { 29 camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalyzer) 30 31 preview?.setSurfaceProvider(fragmentCameraBinding.viewFinder.surfaceProvider) 32 } catch (exc: Exception) { 33 Log.e(TAG, "Use case binding failed", exc) 34 } 35 } 36 37 private fun detectObjects(image: ImageProxy) { 38 image.use { bitmapBuffer.copyPixelsFromBuffer(image.planes[0].buffer) } 39 imageRotation = image.imageInfo.rotationDegrees 40 41 if (!Python.isStarted()) { 42 Python.start(AndroidPlatform(requireContext())) 43 } 44 .......... 45 ................... 46 ............................ 47 //**ここはjavaでBitmapをByteArrayに変換 48 * ByteArrayOutputStream baos = new ByteArrayOutputStream(); 49 * bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 50 * byte[] imageBytes = baos.toByteArray(); 51 *// 52 ............................. 53 .................. 54 ........... 55 56 var py = Python.getInstance().getModule("cv2_module") 57 //* PyObjectで戻ってくる*// 58 var contour_img = module.callAttr("Cv2_Module", imagerotation, imageBytes).toString() 59 60 "contour_imgをデコードし、Bitmapに変換ののちCanvasで描写" 61 }
cv2_module
1def Cv2_Module2(rotation,): 2 3 np_data = np.frombuffer(byteA,np.uint8) 4 img = cv2.imdecode(np_data,cv2.IMREAD_UNCHANGED) 5 img_gray = cv2.cvtColor(img,cv2.COLOR_RGBA2GRAY) 6 7 ret, thresh = cv2.threshold(img_gray, 120, 255, cv2.THRESH_BINARY) 8 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 9 10 contourimg = cv2.drawContours(img, contours, -1, (0,255,0), 3) 11 resized = cv2.resize(contourimg,dsize=(int(width*1.5), height)) 12 pil_im = Image.fromarray(resized) 13 buff = io.BytesIO() 14 pil_im.save(buff,format="PNG") 15 img_str = base64.b64encode(buff.getvalue()) 16 17 return ""+str(img_str,'utf-8')
あなたの回答
tips
プレビュー