63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class KlasifikasiController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('welcome');
|
|
}
|
|
|
|
public function prediksi(Request $request)
|
|
{
|
|
$request->validate([
|
|
'gambar' => 'required|image'
|
|
]);
|
|
|
|
$gambar = $request->file('gambar');
|
|
$uploadPath = public_path('uploads');
|
|
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0755, true);
|
|
}
|
|
|
|
$namaFile = time() . '.' . $gambar->getClientOriginalExtension();
|
|
$gambar->move($uploadPath, $namaFile);
|
|
$pathGambar = $uploadPath . DIRECTORY_SEPARATOR . $namaFile;
|
|
|
|
try {
|
|
$response = Http::timeout(60)
|
|
->attach('gambar', file_get_contents($pathGambar), $namaFile)
|
|
->post('http://127.0.0.1:5000/prediksi');
|
|
|
|
if (!$response->successful()) {
|
|
$message = 'Server prediksi gagal: ' . $response->status();
|
|
Log::error($message, ['body' => $response->body()]);
|
|
|
|
return back()->with([
|
|
'error' => 'Tidak dapat memproses prediksi. Coba lagi nanti.',
|
|
]);
|
|
}
|
|
|
|
$hasil = $response->json();
|
|
|
|
return back()->with([
|
|
'success' => 'Prediksi berhasil',
|
|
'gambar' => $namaFile,
|
|
'hasil' => $hasil['penyakit'] ?? 'Tidak ada hasil',
|
|
'confidence' => $hasil['confidence'] ?? 0,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Prediksi request error: ' . $e->getMessage());
|
|
|
|
return back()->with([
|
|
'error' => 'Terjadi kesalahan saat menghubungi server prediksi.',
|
|
]);
|
|
}
|
|
}
|
|
} |