-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
158 lines (143 loc) · 4.75 KB
/
main.dart
File metadata and controls
158 lines (143 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
File? _image;
String _resultText = "Sonuç bekleniyor...";
final ImagePicker _picker = ImagePicker();
// Eğer emülatörde çalışıyorsanız, '10.0.2.2' kullanın
// Gerçek cihazda çalışıyorsanız, bilgisayarınızın yerel IP adresini kullanın
String apiUrl = "http://XXX.XXX.XX.X:YYYY/predict"; // Bilgisayarınızın IP adresi ve Flask sunucu portu
bool _isLoading = false; // Yükleniyor durumu
// Fotoğraf seçimi için galeri açma
Future<void> _pickImage() async {
final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
_resultText = "Fotoğraf seçildi, şimdi API'ye gönderin.";
} else {
_resultText = "Fotoğraf seçilmedi.";
}
});
}
// Fotoğrafı API'ye gönderme
Future<void> _sendImageToApi() async {
if (_image == null) {
setState(() {
_resultText = "Lütfen önce bir fotoğraf seçin.";
});
return;
}
setState(() {
_isLoading = true; // Yükleniyor durumunu başlat
_resultText = "Sonuç bekleniyor...";
});
try {
var request = http.MultipartRequest(
'POST',
Uri.parse(apiUrl),
);
// Fotoğraf dosyasını ekle
request.files.add(
await http.MultipartFile.fromPath('image', _image!.path), // Flask 'image' parametresi
);
// API'ya isteği gönder ve yanıtı al
var response = await request.send();
print("Status Code: ${response.statusCode}");
if (response.statusCode == 200) {
var responseData = await http.Response.fromStream(response);
var result = json.decode(responseData.body);
// API yanıtını yazdır
print("Response Body: ${responseData.body}");
// Tahmin sonucunu ekrana yazdır
setState(() {
_resultText = result['result'] == "hasarlı"
? "Fotoğrafta hasar var."
: "Fotoğrafta hasar yok.";
});
} else {
// Başarısız olursa hata kodunu ekrana yazdır
setState(() {
_resultText = "API'dan cevap alınamadı. Hata kodu: ${response.statusCode}";
});
}
} catch (error) {
// Hata durumunda hata mesajını yazdır
print("Hata: $error");
setState(() {
_resultText = "Bir hata oluştu: $error";
});
} finally {
setState(() {
_isLoading = false; // Yükleniyor durumunu sonlandır
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Hasar Algılama"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Fotoğrafın gösterileceği ImageView
_image != null
? Image.file(
_image!,
width: 300,
height: 300,
)
: Container(
width: 300,
height: 300,
color: Colors.grey[300],
child: Icon(
Icons.image,
size: 100,
color: Colors.grey[800],
),
),
SizedBox(height: 20),
// Yükleniyor göstergesi
_isLoading ? CircularProgressIndicator() : SizedBox.shrink(),
SizedBox(height: 20),
// 1. Tuş: Fotoğraf seçimi
ElevatedButton(
onPressed: _pickImage,
child: Text("Fotoğraf Seç"),
),
SizedBox(height: 20),
// 2. Tuş: Fotoğrafı API'ye gönder
ElevatedButton(
onPressed: _image == null ? null : _sendImageToApi, // Butonu deaktive etme
child: Text("API'ye Gönder"),
),
SizedBox(height: 20),
// API sonucu için text alanı
Text(
_resultText,
style: TextStyle(fontSize: 18),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}