-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access camera with OpenCV #303
Comments
@mhsmith Thank you ! Do you have any ideas to solve this ? |
Currently the easiest way to pass images between Java/Kotlin and Python is to pass them as JPG or PNG data. Here's an example of a project that does that: Or if you want to pass an image from Python to Java/Kotlin and display it on the screen, see the chaquopy-matplotlib project. |
@mhsmith First thanks for your ideas, but I'm new to Kotlin and don't want to use YUV.
` In the new_test.py:
` And I got this error: `
` |
I also tried this:
the image_test.py is :
got another error: com.chaquo.python.PyException: TypeError: an integer is required (got type [B) |
In Python, the arguments to
|
I've tried convert Mat to Byte in JAVA like this:
and passing byteArray to python like this:
in Python code like this:
I still got an error: |
This is a common Numpy error message: please search and you'll find plenty explanations of it. I think you have successfully converted the image, but you should test it with |
If this is still a problem, please reopen the issue and give details. |
@mhsmith Hello,now I can pass java's matofbyte to python and got some data like this:
|
If the first channel is always 255, then it's probably an alpha channel. You can remove it like this:
|
As for the exception, I don't think it can come from the code you posted. If you still can't work it out, please post the code around the indicated line number (new_test.py:80). |
Unlike most Python containers, NumPy arrays can't usually be converted to a boolean value. If you want to compare |
@mhsmith It works,Thank you!, now I have changed my code into:
and in new_test.py:
The app is always working but I get a black screen. I found this in the log:
Is it not correct in my code to passing the frame or in java code decode the image data? |
The image returned from Python is in PNG format, so you'll have to decode it using the OpenCV Java API. I'm sure you can find examples of this by searching. |
Hi , I tried Camera2 api like you metioned in this: |
It looks like the Camera2 API implementation in that app is incomplete, as it doesn't encode the image to JPEG before passing it to Python. You'll have to do that using something equivalent to the Camera API example. |
@mhsmith W/python.stderr: Unlicensed copy of Chaquopy: app will now shut down. |
You didn't mention your GitHub username, so I don't know which email was yours, but I've answered all the emails currently in my inbox. |
@mhsmith Thanks, I've received your email. Our project have the last problem is that these three lines code:
It's very slow to running these three lines. Is there any solutions to speed it up? |
Your |
@mhsmith Actually I have tried just do nothing except passing data to python then decode in java just like this:
and it's still 1075ms , Is there any solution? |
I think the slow part is probably the call to The fastest way to return bytes from a Python function is to return a |
@mhsmith Hi, I'm sorry to reply late. We have talked with email. And this is my boss's problem:
and this is the stdout in AndroidStudio, the time's unit is second:
But when it display to the screen,It is really slow(Accoring to our eys, maybe only 0.1~1 fps). How can we speed up?
we can get this in AS:
the screen shows fps is 20,but our eyes see the fps is only 1~2. |
What error? If
Then the slow part must be somewhere other than the In your previous comment you found that |
@mhsmith oh,we don't convert it to a bytes, so we get an error,now we can use without base64.
and the log is:
but the screen is really slow. What is our problem? |
Actually, you don't even need to use |
@mhsmith Thanks !We use timestamps found that:
This code using time is 150~170 ms.
This code usting time is 0.8~1.2s We test our python code like this and finally found that:
frame = np.array(frame) using too much time, almost about 600ms~800ms .Just as my previous comment said. How could we optimize our code? |
The most obvious idea I can think of is to try using a resolution lower than 1920x1080. That would speed up all of your image processing code. Having said that, 600-800 ms is surprisingly slow for loading and copying a JPEG image, even if it is such high resolution. If you find that this code runs significantly slower on Chaquopy than on other platforms, with identical image files, then please send me a test file along with its performance measurements on both platforms, and I'll look into it. Otherwise, your problem is probably with Pillow or Numpy rather than Chaquopy, so if you need any further help on this, I think you should look elsewhere. |
@mhsmith Thanks, our engineer will find the slow reason. When we can run it fluently, we will ask for a license. Hope we can keep talking. Best Wishes! |
@chaotianjiao Hello, have you found the solution? I'm facing the same problem of passing Mat to python now... Could you please possibly share your solution with me? |
Unfortunately I don't know any easy way for the OpenCV Java and Python APIs to interact directly. The simplest solution is to pass data between them as a PNG file, as described above. |
Originally posted by @chaotianjiao in #140 (comment)
Hi, How do you pass the frames to Python? Can you give some examples?
Acturally, I use opencv-android-sdk to get a camera frame,like this:
And I have a test.py like this:
`
def test_video(path):
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
start = timer()
pro_frame = process(frame, size=4)
end = timer()
`
If we can't use cv2.VideoCapture,cv2.imshow(), How can the java frame passing to test_video() ?
If the previous step success, How can you show the result in an Android app(Using SurfaceView or sth else?)
I will be very appreciated if you can answer my questions above.
The text was updated successfully, but these errors were encountered: