Advertisement
jbn6972

Untitled

Apr 19th, 2023
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.33 KB | None | 0 0
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:image/image.dart' as imglib;
  4.  
  5. // Load the image from file
  6. File imageFile = File('path/to/image.jpg');
  7. imglib.Image image = imglib.decodeJpg(imageFile.readAsBytesSync());
  8.  
  9. // Resize the image to the input size of the TFLite model
  10. imglib.Image resizedImage = imglib.copyResize(image, width: 150, height: 150);
  11.  
  12. // Convert the image pixel values to a normalized Uint8List
  13. Uint8List inputValues = Uint8List(150 * 150 * 3);
  14. for (int i = 0; i < 150; i++) {
  15.   for (int j = 0; j < 150; j++) {
  16.     int pixel = resizedImage.getPixel(j, i);
  17.     inputValues[i * 150 * 3 + j * 3 + 0] = ((pixel >> 16) & 0xFF);
  18.     inputValues[i * 150 * 3 + j * 3 + 1] = ((pixel >> 8) & 0xFF);
  19.     inputValues[i * 150 * 3 + j * 3 + 2] = (pixel & 0xFF);
  20.   }
  21. }
  22. inputValues = inputValues.buffer.asUint8List();
  23.  
  24. // Alternatively, you can convert the image pixel values to a normalized Float32List
  25. Float32List inputValuesFloat = Float32List(150 * 150 * 3);
  26. for (int i = 0; i < 150; i++) {
  27.   for (int j = 0; j < 150; j++) {
  28.     int pixel = resizedImage.getPixel(j, i);
  29.     inputValuesFloat[i * 150 * 3 + j * 3 + 0] = ((pixel >> 16) & 0xFF) / 255.0;
  30.     inputValuesFloat[i * 150 * 3 + j * 3 + 1] = ((pixel >> 8) & 0xFF) / 255.0;
  31.     inputValuesFloat[i * 150 * 3 + j * 3 + 2] = (pixel & 0xFF) / 255.0;
  32.   }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement