204 lines
5.5 KiB
PHP
204 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Models\Prediksi;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
|
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()) {
|
|
|
|
Log::error('Server prediksi gagal', [
|
|
'body' => $response->body()
|
|
]);
|
|
|
|
return back()->with([
|
|
'error' => 'Tidak dapat memproses prediksi.'
|
|
]);
|
|
}
|
|
|
|
$hasil = $response->json();
|
|
$allPredictions = $hasil['all_predictions'] ?? [];
|
|
$deskripsi = '';
|
|
$solusi = '';
|
|
|
|
if ($hasil['penyakit'] == 'Blast') {
|
|
|
|
$deskripsi = 'Blast adalah penyakit daun padi akibat jamur Pyricularia oryzae yang menyebabkan bercak berbentuk belah ketupat.';
|
|
|
|
$solusi = 'Gunakan fungisida, kurangi kelembaban lahan, dan gunakan varietas tahan penyakit.';
|
|
}
|
|
|
|
elseif ($hasil['penyakit'] == 'Blight') {
|
|
|
|
$deskripsi = 'Blight merupakan penyakit hawar daun bakteri yang menyebabkan daun menguning dan mengering.';
|
|
|
|
$solusi = 'Gunakan benih sehat, lakukan sanitasi lahan, dan gunakan bakterisida.';
|
|
}
|
|
|
|
elseif ($hasil['penyakit'] == 'Tungro') {
|
|
|
|
$deskripsi = 'Tungro adalah penyakit virus pada tanaman padi yang ditularkan oleh wereng hijau.';
|
|
|
|
$solusi = 'Kendalikan wereng hijau dan gunakan varietas tahan tungro.';
|
|
}
|
|
|
|
elseif ($hasil['penyakit'] == 'Healthy') {
|
|
|
|
$deskripsi = 'Daun padi dalam kondisi sehat dan tidak terdeteksi penyakit.';
|
|
|
|
$solusi = 'Pertahankan pola tanam dan perawatan yang baik.';
|
|
}
|
|
|
|
Prediksi::create([
|
|
'gambar' => $namaFile,
|
|
'penyakit' => $hasil['penyakit'],
|
|
'confidence' => $hasil['confidence']
|
|
]);
|
|
|
|
return back()->with([
|
|
'success' => 'Prediksi berhasil',
|
|
'gambar' => $namaFile,
|
|
'hasil' => $hasil['penyakit'] ?? 'Tidak ada hasil',
|
|
'confidence' => $hasil['confidence'] ?? 0,
|
|
'deskripsi' => $deskripsi,
|
|
'solusi' => $solusi,
|
|
'allPredictions' => $allPredictions,
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error(
|
|
'Prediksi request error: ' . $e->getMessage()
|
|
);
|
|
|
|
return back()->with([
|
|
'error' => 'Terjadi kesalahan saat menghubungi server prediksi.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
// ===== RIWAYAT =====
|
|
|
|
public function riwayat(Request $request)
|
|
{
|
|
$query = Prediksi::query();
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where('penyakit', 'like', '%' . $request->search . '%');
|
|
}
|
|
|
|
if ($request->filled('filter')) {
|
|
$query->where('penyakit', $request->filter);
|
|
}
|
|
|
|
$data = $query->latest()->get();
|
|
|
|
return view('riwayat', compact('data'));
|
|
}
|
|
|
|
// ===== DASHBOARD =====
|
|
|
|
public function dashboard()
|
|
{
|
|
$totalPrediksi = Prediksi::count();
|
|
|
|
$blast = Prediksi::where('penyakit', 'Blast')->count();
|
|
|
|
$blight = Prediksi::where('penyakit', 'Blight')->count();
|
|
|
|
$healthy = Prediksi::where('penyakit', 'Healthy')->count();
|
|
|
|
$tungro = Prediksi::where('penyakit', 'Tungro')->count();
|
|
|
|
$terbaru = Prediksi::latest()->take(5)->get();
|
|
|
|
return view('dashboard', compact(
|
|
'totalPrediksi',
|
|
'blast',
|
|
'blight',
|
|
'healthy',
|
|
'tungro',
|
|
'terbaru'
|
|
));
|
|
}
|
|
|
|
public function exportPdf()
|
|
{
|
|
$data = Prediksi::latest()->get();
|
|
|
|
$pdf = Pdf::loadView(
|
|
'pdf.laporan',
|
|
compact('data')
|
|
);
|
|
|
|
return $pdf->download(
|
|
'laporan-prediksi.pdf'
|
|
);
|
|
}
|
|
public function hapus($id)
|
|
{
|
|
$data = Prediksi::findOrFail($id);
|
|
|
|
$path = public_path('uploads/' . $data->gambar);
|
|
|
|
if (file_exists($path)) {
|
|
unlink($path);
|
|
}
|
|
|
|
$data->delete();
|
|
|
|
return redirect()->back()->with(
|
|
'success',
|
|
'Data berhasil dihapus'
|
|
);
|
|
}
|
|
|
|
public function statistik()
|
|
{
|
|
return response()->json([
|
|
'blast' => Prediksi::where('penyakit', 'Blast')->count(),
|
|
'blight' => Prediksi::where('penyakit', 'Blight')->count(),
|
|
'healthy' => Prediksi::where('penyakit', 'Healthy')->count(),
|
|
'tungro' => Prediksi::where('penyakit', 'Tungro')->count(),
|
|
]);
|
|
}
|
|
} |