MIF_E31230393/app/Http/Controllers/ClassificationController.php

130 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Classification;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class ClassificationController extends Controller
{
public function process(Request $request)
{
// 🔹 Ambil base64 dari frontend
$imageData = $request->input('image');
$image = str_replace('data:image/png;base64,', '', $imageData);
$image = str_replace(' ', '+', $image);
// 🔹 Simpan sementara
$imageName = 'temp_' . time() . '.png';
Storage::disk('public')->put('scans/' . $imageName, base64_decode($image));
// 🔹 Path file untuk Python
$imagePath = storage_path('app/public/scans/' . $imageName);
// 🔹 Path Python & script
$python = "C:\\Program Files\\Python310\\python.exe";
$script = base_path('python/predict.py');
// 🔹 Jalankan Python
$command = "\"$python\" \"$script\" \"$imagePath\"";
$output = shell_exec($command);
// 🔥 HANDLE ERROR DARI PYTHON
if (!$output || str_contains($output, 'ERROR')) {
return response()->json([
'status' => 'error',
'message' => 'Gagal memproses gambar (objek tidak terdeteksi)'
], 500);
}
// 🔹 Format output: label|confidence
$result = explode('|', trim($output));
if (count($result) < 2) {
return response()->json([
'status' => 'error',
'message' => 'Format output Python tidak valid'
], 500);
}
$label = $result[0];
$confidence = round($result[1] * 100, 2);
// 🔹 Mapping label biar lebih user-friendly
$labelMap = [
'light' => 'Light Roast',
'medium' => 'Medium Roast',
'dark' => 'Dark Roast'
];
$finalLabel = $labelMap[$label] ?? $label;
return response()->json([
'status' => 'success',
'label' => $finalLabel,
'confidence' => $confidence . '%',
'image_name' => $imageName,
'image_url' => asset('storage/scans/' . $imageName),
'created_at' => now()->translatedFormat('d F Y, H:i') . ' WIB'
]);
}
public function store(Request $request)
{
Classification::create([
'user_id' => Auth::id(),
'image_path' => 'scans/' . $request->image_name,
'label' => $request->label,
'confidence' => str_replace('%', '', $request->confidence),
'color_data' => json_encode([
'r' => 120,
'g' => 80,
'b' => 50
])
]);
return response()->json(['message' => 'Data berhasil disimpan ke riwayat!']);
}
public function destroyTemp(Request $request)
{
$imagePath = 'scans/' . $request->image_name;
if (Storage::disk('public')->exists($imagePath)) {
Storage::disk('public')->delete($imagePath);
return response()->json(['message' => 'File sampah berhasil dibuang!']);
}
return response()->json(['message' => 'File tidak ditemukan'], 404);
}
public function history()
{
$histories = Classification::where('user_id', Auth::id())
->orderBy('created_at', 'desc')
->get();
return view('history', compact('histories'));
}
public function destroy($id)
{
$data = Classification::where('id', $id)
->where('user_id', Auth::id())
->firstOrFail();
if (Storage::disk('public')->exists($data->image_path)) {
Storage::disk('public')->delete($data->image_path);
}
$data->delete();
return redirect()->route('history')->with('success', 'Data riwayat berhasil dihapus.');
}
public function karakteristik()
{
return view('karakteristik');
}
}