I'm making an app using OpenCV face detection. I want to make some changes to the UI when a face is detected in the camera image.
The layout is split in two parts, on the left some text and camera image on the right. I want to change the text color when a face is detected.
I detect the faces in the onCameraFrame() method with no problems, but if I try to change the UI elements from this method the app crashes.
Here's how it all looks. /* Not real code, just example */
public class MyClass extends Activity implements CvCameraViewListener
{
private CameraBridgeViewBase mOpenCvCameraView;
private TextView myTextElement;
private FaceLocator faceLocator;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myclass);
myTextElement = (TextView) findViewById(R.id.text_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.my_class_face_detector_layout);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
}
@Override
public Mat onCameraFrame(Mat inputFrame)
{
if (faceLocator != null) {
bool face_detected = faceLocator.detectFaces(inputFrame);
if (face_detected) {
myTextElement.setTextColor(Color.GREEN);
}
}
return inputFrame;
}
}
Can anyone help?
Here's the stack trace...
at android.view.ViewRootImpl.checkThread
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1005)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4548)
at android.view.View.invalidate(View.java:11095)
at android.view.View.invalidate(View.java:11044)
at android.widget.TextView.updateTextColors(TextView.java:3473)
at android.widget.TextView.setTextColor(TextView.java:2663)
at com.riteh.mateo.cameracontrol.CCSettings.onCameraFrame(CCSettings.java:186)
at org.opencv.android.CameraBridgeViewBase$CvCameraViewListenerAdapter.onCameraFrame(CameraBridgeViewBase.java:157)
at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:393)
at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:346)
at java.lang.Thread.run(Thread.java:841)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…