Skip to content

Commit 3908858

Browse files
send pictures from frontend to backend
1 parent 7af8f41 commit 3908858

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
API_BASE_URL=http://192.168.0.1:5000
2+
3+
# the phone you run the app on must be conected to the same Wi-Fi as your computer
4+
5+
# on Windows:
6+
# `ipconfig`
7+
# go to "Wireless LAN adapter Wi-Fi:"
8+
# choose the "IPv4 Address" address
9+
10+
# or run the server and Flask will tell you which IP addresses it uses

sign_language_translator/lib/components/camera.dart

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'dart:async'; //pentru a folosi Timer-ul
2-
import 'dart:typed_data'; //pentru a manipula fișierele în format de bytes
32
import 'package:camera/camera.dart';
43
import 'package:flutter/material.dart';
5-
import 'package:http/http.dart' as http; //pachetul http pentru a trimite cereri
6-
import 'dart:convert'; // pentru json
7-
import 'package:flutter_dotenv/flutter_dotenv.dart';
4+
import 'package:sign_language_translator/services/network.dart';
85

96
class Camera extends StatefulWidget {
107
const Camera({super.key});
8+
119
@override
1210
State<Camera> createState() => CameraState();
1311
}
@@ -16,9 +14,10 @@ class CameraState extends State<Camera> {
1614
late List<CameraDescription> cameras;
1715
late CameraController cameraController;
1816
bool isRearCamera = true;
19-
Timer?
20-
_timer; // un timer pentru a putea trimite imaginile din cateva milisecunde
21-
// in cateva milisecunde.
17+
Timer? _timer; // Timer pentru trimiterea imaginilor
18+
19+
// Initialize Network instance
20+
final Network network = Network();
2221

2322
void startCamera(int camera) async {
2423
cameraController = CameraController(
@@ -133,72 +132,22 @@ class CameraState extends State<Camera> {
133132
try {
134133
final image = await cameraController.takePicture();
135134
final imageBytes = await image.readAsBytes();
136-
await sendImageToServer(imageBytes);
135+
await network.sendImageToServer(imageBytes);
137136
} catch (e) {
138137
print('Error in image processing: $e');
139138
}
140139
}
141140
});
142141
}
143142

144-
// Metoda pentru resetarea procesului de trimitere al imaginilor.
145143
void startOrResetTranslation() {
146-
_startSendingImages(); // trimit imaginile
147-
}
148-
149-
List<String> translationOutputs = []; // lista pentru a pastra outputurile.
150-
Future<void> sendImageToServer(Uint8List imageBytes) async {
151-
final baseUrl = dotenv.env['API_BASE_URL'];
152-
final url = Uri.parse('$baseUrl/upload');
153-
154-
try {
155-
// Verific daca imaginea a fost capturata, prin afisarea dimensiunii la consola.
156-
print('Image size: ${imageBytes.length} bytes');
157-
158-
// Creăm cererea Multipart
159-
var request = http.MultipartRequest('POST', url);
160-
// Adaug fisierul la request.
161-
request.files.add(http.MultipartFile.fromBytes('file', imageBytes,
162-
filename: 'image.jpg'));
163-
print("Adding file to request...");
164-
var response = await request.send();
165-
print("Sending request...");
166-
if (response.statusCode == 200) {
167-
final responseBody = await response.stream.bytesToString();
168-
print('Server response: $responseBody');
169-
} else {
170-
print('Failed to send the image: ${response.statusCode}');
171-
final responseBody = await response.stream.bytesToString();
172-
print('Error response body: $responseBody');
173-
}
174-
} catch (e) {
175-
print('Error sending image: $e');
176-
}
144+
_startSendingImages();
177145
}
178146

179147
void stopTranslationAndSendToLLM() {
180-
_timer?.cancel(); // Opresc trimiterea imaginilor
181-
// Trimit output-urile salvate spre procesarea cu modelul AI.
182-
sendOutputsToLLM();
148+
_timer?.cancel();
149+
network.sendOutputsToLLM(translationOutputs);
183150
}
184151

185-
Future<void> sendOutputsToLLM() async {
186-
final baseUrl = dotenv.env['API_BASE_URL'];
187-
final url = Uri.parse('$baseUrl/processing_translate');
188-
189-
try {
190-
var response = await http.post(
191-
url,
192-
headers: {'Content-Type': 'application/json'},
193-
body: jsonEncode({'translations': translationOutputs}),
194-
);
195-
196-
if (response.statusCode == 200) {
197-
print('Procesarea a fost incheiata cu succes!');
198-
print('answer from LLM: ${response.body}');
199-
}
200-
} catch (e) {
201-
print('Eroare: $e');
202-
}
203-
}
152+
List<String> translationOutputs = [];
204153
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'dart:typed_data'; // pentru a manipula fișierele în format de bytes
2+
import 'dart:convert'; // pentru json
3+
import 'package:http/http.dart' as http;
4+
import 'package:flutter_dotenv/flutter_dotenv.dart';
5+
6+
class Network {
7+
final _baseUrl = dotenv.env['API_BASE_URL'];
8+
9+
Future<void> sendImageToServer(Uint8List imageBytes) async {
10+
final url = Uri.parse('$_baseUrl/upload');
11+
12+
try {
13+
print('Image size: ${imageBytes.length} bytes');
14+
15+
var request = http.MultipartRequest('POST', url);
16+
request.files.add(http.MultipartFile.fromBytes('file', imageBytes,
17+
filename: 'image.jpg'));
18+
19+
var response = await request.send();
20+
print("Sending request...");
21+
if (response.statusCode == 200) {
22+
final responseBody = await response.stream.bytesToString();
23+
print('Server response: $responseBody');
24+
} else {
25+
print('Failed to send the image: ${response.statusCode}');
26+
final responseBody = await response.stream.bytesToString();
27+
print('Error response body: $responseBody');
28+
}
29+
} catch (e) {
30+
print('Error sending image: $e');
31+
}
32+
}
33+
34+
Future<void> sendOutputsToLLM(List<String> translationOutputs) async {
35+
final url = Uri.parse('$_baseUrl/processing_translate');
36+
37+
try {
38+
var response = await http.post(
39+
url,
40+
headers: {'Content-Type': 'application/json'},
41+
body: jsonEncode({'translations': translationOutputs}),
42+
);
43+
44+
if (response.statusCode == 200) {
45+
print('Procesarea a fost incheiata cu succes!');
46+
print('answer from LLM: ${response.body}');
47+
}
48+
} catch (e) {
49+
print('Eroare: $e');
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)