TIF_E41210178/app/Http/Controllers/Api/RecognitionController.php

66 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Hewan;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
class RecognitionController extends Controller
{
public function predictHewan(Request $request)
{
$image = $request->file('image');
$response = Http::attach(
'image',
file_get_contents($image->getPathname()),
$image->getClientOriginalName()
)->post('http://127.0.0.1:5000/predict/hewan');
$data = json_decode($response->body(), true);
$hasil = $data['prediction'];
$confidence = $data['confidence'];
$hewan = Hewan::where('name', $hasil)->first();
$url = url('images/foto/' . $hewan->foto);
$hewan->foto = $url;
$confiden = round($confidence * 100, 2) . '%';
$hewan->confidence = $confiden;
// ambi nama dari tabel kategori, pluck id kategori
$kategori = $hewan->kategori()->pluck('name')->first();
$hewan->kategori = $kategori;
return response()->json([
'status' => 'success',
'message' => 'Image successfully detected!',
'data' => $hewan,
]);
}
public function predictHewan2(Request $request)
{
$image = $request->file('image');
$response = Http::attach(
'image',
file_get_contents($image->getPathname()),
$image->getClientOriginalName()
)->post('http://127.0.0.1:5000/predict/hewan');
$data = json_decode($response->body(), true);
$hasil = $data['prediction'];
$confidence = $data['confidence'];
return response()->json([
'status' => 'success',
'message' => 'Image successfully detected!',
'prediction' => $hasil,
'confidence' => $confidence,
]);
}
}