I have issues with data type when trying to give colors to draw lines between the detected body parts by posedetection!
I want the code to draw lines between the detected parts of the body without facing a problem with the color type. I think the problem accuors when i try to use color from image library in flutter and i don’t know how to solve it.
I have tried multible things, but one and last one was the following:
import 'dart:io';
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:google_mlkit_pose_detection/google_mlkit_pose_detection.dart';
import 'package:path_provider/path_provider.dart';
class PoseDetection {
static final PoseDetector _poseDetector = PoseDetector(
options: PoseDetectorOptions(
mode: PoseDetectionMode.single,
),
);
static Future<void> detectPoses(String videoPath) async {
final outputDirectory = await getApplicationDocumentsDirectory();
final directory = Directory(outputDirectory.path);
List<File> frameFiles = directory.listSync()
.where((entity) => entity is File && entity.path.endsWith('.jpg') && entity.path.contains('frame'))
.map((entity) => entity as File)
.toList();
for (File frameFile in frameFiles) {
final inputImage = InputImage.fromFile(frameFile);
final List<Pose> poses = await _poseDetector.processImage(inputImage);
img.Image? originalImage = img.decodeImage(frameFile.readAsBytesSync());
for (final pose in poses) {
var landmarks = pose.landmarks;
// Create a list of points representing the body parts
final List<Point<int>> bodyPoints = [
for (var landmark in landmarks.values)
Point(landmark.x.toInt(), landmark.y.toInt())
];
// Draw lines connecting the body parts
drawPolyline(originalImage!, bodyPoints, Colors.blue);
}
// Save the modified image back to disk
File('${frameFile.path}_pose.jpg').writeAsBytesSync(img.encodeJpg(originalImage!));
}
}
static void drawPolyline(img.Image image, List<Point<int>> points, Color color) {
final List<Point<int>> pointsClosed = List.from(points)..add(points.first);
for (int i = 0; i < pointsClosed.length - 1; i++) {
final Point<int> currentPoint = pointsClosed[i];
final Point<int> nextPoint = pointsClosed[i + 1];
_drawLine(image, currentPoint.x, currentPoint.y, nextPoint.x, nextPoint.y, color);
}
}
static void _drawLine(img.Image image, int x1, int y1, int x2, int y2, Color color) {
while (true) {
image.setPixel(x1, y1, color);
if (x1 == x2 && y1 == y2) break;
final int dx = (x2 - x1).abs(), sx = x1 < x2 ? 1 : -1;
final int dy = -(y2 - y1).abs(), sy = y1 < y2 ? 1 : -1;
int err = dx + dy, e2; /* error value e_xy */
e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x1 += sx;
} /* e_xy+e_x > 0 */
if (e2 <= dx) {
err += dx;
y1 += sy;
} /* e_xy+e_y < 0 */
}
}
static void dispose() {
_poseDetector.close();
}
}
user24838206 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.