I am using Android Studio and Kotlin to develop an android application, part of this involves extracting video frames, and filtering the relevant ones. I am using OpenCV to extract the frames. So, I am running the code for OpenCV using python. When I try to pass the path of the video file from MainActivity.kt to the python file for extracting frames, I come across the error:
com.chaquo.python.PyException: TypeError: Argument 'index' is required to be an integer
My MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val py = Python.getInstance()
val module = py.getModule("opencvtest")
val cvRes = module["FrameCapture"]
val path = Uri.parse("android.resource://" + packageName + "/" + R.raw.video2)
val a = cvRes?.call(path)
println("The value of a is $a")
}
}
My python file:
import cv2
def FrameCapture(path):
vidObj = cv2.VideoCapture(path)
fps = int(vidObj.get(cv2.CAP_PROP_FPS))
frame_count = int(vidObj.get(cv2.CAP_PROP_FRAME_COUNT))
count = 1
frames_arr = []
for i in range(int(frame_count/fps)+1):
arr_frame = []
arr_lap = []
for j in range(fps):
success, frame = vidObj.read()
if success:
laplacian = cv2.Laplacian(frame, cv2.CV_64F).var()
arr_lap.append(laplacian)
arr_frame.append(frame)
selected_frame = arr_frame[arr_lap.index(max(arr_lap))]
frames_arr.append(selected_frame)
count = count + 1
cv2.imwrite("frame1.jpg", frames_arr[0])
cv2.imwrite("frame2.jpg", frames_arr[count-2])
return True
I have noticed that the error comes on the line, vidObj = cv2.VideoCapture(path)
.
This means it is expecting the argument for VideoCapture to be an integer called “index”.
Then I searched online to find
So there are different implementations of VideoCapture, and my code expects the argument index.
The path to my video file is : android.resource://com.example.videotrail1/2131951616 (in case the problem is due to the way it is specified)
I used chaquopy to support python on Android Studio, and Python version : 3.8
Any help regarding the error, or any alternate and efficient methods to use OpenCV in Android Studio are appreciated.