How come the android face detection detects my face on the phone but the system server doesn’t detect it?

I am trying to get face recognition to work on this android app I am making. On my phone it says the face has been detected but the system_server says it has not been detected.
Logcat messages I get on Android Studio

I am making this making this android app that needs to communicate with a laravel website and authenticate users with their faces. It is supposed to send this data to a MySQL server on the laravel website and verify if their face exists and if there is no image it saves their face and if it’s not the same they can’t clock in but the app can’t seem to detect my face on the backend. I am mlkit face detection on android and then retrofit to send the requests to the laravel website and then face++ API to check if the faces match. But when I try and use my face it says face detected and doesn’t do anything after and then the app says it’s taking too long to respond. So I checked the logs on Android Studio and saw that the “SmartStayController” is saying faceDetected:false and I’m not sure that means or how to fix it.

package com.example.clockinclockout

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.ByteArrayOutputStream
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

class FaceRecognitionActivity : AppCompatActivity() {

    private lateinit var previewView: PreviewView
    private lateinit var captureButton: Button
    private lateinit var imageCapture: ImageCapture
    private lateinit var cameraExecutor: ExecutorService

    private var companyId: Int = 0
    private var employeeId: Int = 0

    companion object {
        private const val REQUEST_CAMERA_PERMISSION = 1001
        private const val TAG = "FaceRecognitionActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_face_recognition)

        companyId = intent.getIntExtra("companyId", 0)
        employeeId = intent.getIntExtra("employeeId", 0)

        previewView = findViewById(R.id.previewView)
        captureButton = findViewById(R.id.captureButton)

        cameraExecutor = Executors.newSingleThreadExecutor()

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
        } else {
            startCamera()
        }

        captureButton.setOnClickListener {
            capturePhoto()
        }
    }

    private fun startCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

        cameraProviderFuture.addListener({
            val cameraProvider = cameraProviderFuture.get()
            val preview = Preview.Builder().build().also {
                it.setSurfaceProvider(previewView.surfaceProvider)
            }

            imageCapture = ImageCapture.Builder().build()

            val cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA

            try {
                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)
            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(this))
    }

    private fun capturePhoto() {
        val imageCapture = imageCapture ?: return

        imageCapture.takePicture(ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageCapturedCallback() {
            override fun onCaptureSuccess(imageProxy: ImageProxy) {
                val bitmap = imageProxyToBitmap(imageProxy)
                imageProxy.close()

                if (bitmap != null) {
                    processFace(bitmap)
                } else {
                    Toast.makeText(this@FaceRecognitionActivity, "Failed to capture image", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onError(exception: ImageCaptureException) {
                Toast.makeText(this@FaceRecognitionActivity, "Capture failed: ${exception.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun imageProxyToBitmap(imageProxy: ImageProxy): Bitmap? {
        val buffer = imageProxy.planes[0].buffer
        val bytes = ByteArray(buffer.capacity())
        buffer.get(bytes)
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
    }

    private fun processFace(bitmap: Bitmap) {
        val image = InputImage.fromBitmap(bitmap, 0)
        val detector = FaceDetection.getClient(
            FaceDetectorOptions.Builder()
                .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
                .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_NONE)
                .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_NONE)
                .build()
        )

        detector.process(image)
            .addOnSuccessListener { faces ->
                if (faces.isNotEmpty()) {
                    Log.d(TAG, "Face detected")
                    Toast.makeText(this, "Face detected", Toast.LENGTH_SHORT).show()
                    checkFaceExists(companyId, employeeId, bitmap)
                } else {
                    Log.d(TAG, "No face detected")
                    Toast.makeText(this, "No face detected", Toast.LENGTH_SHORT).show()
                }
            }
            .addOnFailureListener {e ->
                Log.e(TAG, "Face detection failed: ${e.message}")
                Toast.makeText(this, "Failed to process face: ${e.message}", Toast.LENGTH_SHORT).show()
            }
    }

    private fun checkFaceExists(companyId: Int, employeeId: Int, bitmap: Bitmap) {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://9820-41-80-113-159.ngrok-free.app/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val api = retrofit.create(ApiService::class.java)
        val byteArrayOutputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
        val imageBytes = byteArrayOutputStream.toByteArray()

        val faceRequest = FaceRequest(companyId, employeeId, imageBytes)

        api.verifyFace(faceRequest).enqueue(object : Callback<VerifyFaceResponse> {
            override fun onResponse(call: Call<VerifyFaceResponse>, response: Response<VerifyFaceResponse>) {
                if (response.isSuccessful) {
                    val verifyResponse = response.body()
                    if (verifyResponse != null && verifyResponse.success) {
                        clockInUser(companyId, employeeId, imageBytes)
                    } else {
                        saveFace(companyId, employeeId, imageBytes)
                    }
                } else {
                    Toast.makeText(this@FaceRecognitionActivity, "Error: ${response.message()}", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<VerifyFaceResponse>, t: Throwable) {
                Toast.makeText(this@FaceRecognitionActivity, "Network error: ${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun saveFace(companyId: Int, employeeId: Int, imageBytes: ByteArray) {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://9820-41-80-113-159.ngrok-free.app/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val api = retrofit.create(ApiService::class.java)
        val faceRequest = FaceRequest(companyId, employeeId, imageBytes)

        api.saveFace(faceRequest).enqueue(object : Callback<SaveFaceResponse> {
            override fun onResponse(call: Call<SaveFaceResponse>, response: Response<SaveFaceResponse>) {
                if (response.isSuccessful) {
                    val saveResponse = response.body()
                    if (saveResponse != null && saveResponse.success) {
                        clockInUser(companyId, employeeId, imageBytes)
                    } else {
                        Toast.makeText(this@FaceRecognitionActivity, "Failed to save face", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Toast.makeText(this@FaceRecognitionActivity, "Error: ${response.message()}", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<SaveFaceResponse>, t: Throwable) {
                Toast.makeText(this@FaceRecognitionActivity, "Network error: ${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun clockInUser(companyId: Int, employeeId: Int, imageBytes: ByteArray) {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://9820-41-80-113-159.ngrok-free.app/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val api = retrofit.create(ApiService::class.java)
        val clockInRequest = ClockInRequest(companyId, employeeId, imageBytes)

        api.clockIn(clockInRequest).enqueue(object : Callback<ClockInResponse> {
            override fun onResponse(call: Call<ClockInResponse>, response: Response<ClockInResponse>) {
                if (response.isSuccessful) {
                    val clockInResponse = response.body()
                    if (clockInResponse != null && clockInResponse.success) {
                        startActivity(Intent(this@FaceRecognitionActivity, SuccessActivity::class.java))
                        finish()
                    } else {
                        Toast.makeText(this@FaceRecognitionActivity, "Failed to clock in", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Toast.makeText(this@FaceRecognitionActivity, "Error: ${response.message()}", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ClockInResponse>, t: Throwable) {
                Toast.makeText(this@FaceRecognitionActivity, "Network error: ${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                startCamera()
            } else {
                Toast.makeText(this, "Camera permission is required", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
public function verifyFace(Request $request)
    {
        $employee = Employee::where('Company_ID', $request->companyId)
            ->where('Employee_ID', $request->employeeId)
            ->first();

        if ($employee && $employee->Image) {
            $savedImage = base64_decode($employee->Image);
            $currentImage = base64_decode($request->image);

            $facesMatch = $this->compareFaces($savedImage, $currentImage);

            return response()->json(['success' => $facesMatch]);
        } else {
            return response()->json(['success' => false]);
        }
    }

    public function saveFace(Request $request)
    {
        $employee = Employee::where('Company_ID', $request->companyId)
            ->where('Employee_ID', $request->employeeId)
            ->first();

        if ($employee) {
            $employee->Image = base64_encode($request->image);
            $employee->save();

            return response()->json(['success' => true]);
        } else {
            return response()->json(['success' => false]);
        }
    }

    private function compareFaces($image1, $image2)
    {
        $client = new Client();
        $response = $client->post('https://api-us.faceplusplus.com/facepp/v3/compare', [
            'multipart' => [
                [
                    'name'     => 'api_key',
                    'contents' => $this->facePlusPlusApiKey
                ],
                [
                    'name'     => 'api_secret',
                    'contents' => $this->facePlusPlusApiSecret
                ],
                [
                    'name'     => 'image_base64_1',
                    'contents' => base64_encode($image1)
                ],
                [
                    'name'     => 'image_base64_2',
                    'contents' => base64_encode($image2)
                ],
            ],
        ]);

        $result = json_decode($response->getBody(), true);

        return isset($result['confidence']) && $result['confidence'] > 80;
    }

New contributor

Shane Mushtaq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật