Puffer Check

Bild Übergabe Check

Noch offen: Merge mit Bildanalyse und Zeichnen

16.12.2024 21:35
This commit is contained in:
Paul-1108 2024-12-16 21:36:19 +01:00
parent 789f9e56f1
commit a7efee2023
4 changed files with 94 additions and 66 deletions

26
.idea/workspace.xml generated
View File

@ -4,12 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"> <list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48">
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/CamPuffer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/CamPuffer.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/CamPuffer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/CamPuffer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ImgAnalyzer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ImgAnalyzer.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/ImgAnalyzer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ImgAnalyzer.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -99,6 +97,14 @@
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-jdk-9823dce3aa75-28b599e66164-intellij.indexing.shared.core-IU-242.23726.103" />
<option value="bundled-js-predefined-d6986cc7102b-5c90d61e3bab-JavaScript-IU-242.23726.103" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">
<task active="true" id="Default" summary="Default task"> <task active="true" id="Default" summary="Default task">
@ -111,6 +117,7 @@
<workItem from="1732113837570" duration="20534000" /> <workItem from="1732113837570" duration="20534000" />
<workItem from="1733327375447" duration="951000" /> <workItem from="1733327375447" duration="951000" />
<workItem from="1734091457440" duration="10746000" /> <workItem from="1734091457440" duration="10746000" />
<workItem from="1734364915397" duration="9202000" />
</task> </task>
<task id="LOCAL-00001" summary="First Commit Test"> <task id="LOCAL-00001" summary="First Commit Test">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -160,7 +167,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1734358037207</updated> <updated>1734358037207</updated>
</task> </task>
<option name="localTasksCounter" value="7" /> <task id="LOCAL-00007" summary="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48">
<option name="closed" value="true" />
<created>1734364131101</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1734364131101</updated>
</task>
<option name="localTasksCounter" value="8" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -184,6 +199,7 @@
<MESSAGE value="Version 27.11" /> <MESSAGE value="Version 27.11" />
<MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" /> <MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
<MESSAGE value="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> <MESSAGE value="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" />
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer&#10;16.12.2024 15.06&#10;Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> <MESSAGE value="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48" />
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer&#10;Bild wird nicht korrekt gespeichert&#10;16.12.2024 16:48" />
</component> </component>
</project> </project>

View File

@ -1,4 +1,5 @@
import org.opencv.core.*; import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture; import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui; import org.opencv.highgui.HighGui;
@ -50,6 +51,8 @@ public class CamPuffer implements Runnable {
} }
Mat frame = new Mat(); Mat frame = new Mat();
capture.read(frame);
hasStartedCapturing = true; hasStartedCapturing = true;
long lastGcTime = System.currentTimeMillis(); long lastGcTime = System.currentTimeMillis();
@ -58,19 +61,15 @@ public class CamPuffer implements Runnable {
while (running) { while (running) {
//long startTime = System.currentTimeMillis(); //long startTime = System.currentTimeMillis();
capture.read(frame); // Bild einlesen capture.read(frame); // Bild einlesen
currentMat = frame;
if (frame.empty()) { if (frame.empty()) {
System.out.println("Fehler: Kein Bild von der Kamera erhalten."); System.out.println("Fehler: Kein Bild von der Kamera erhalten.");
break; break;
} }
// Bildverarbeitung hier
updateCurrentMat(frame); updateCurrentMat(frame);
// Debug-Ausgabe
//long elapsedTime = System.currentTimeMillis() - startTime;
//System.out.println("Verarbeitungszeit für das Bild: " + elapsedTime + " ms");
// Speicherbereinigung alle 1000 ms (1 Sekunde) // Speicherbereinigung alle 1000 ms (1 Sekunde)
if (System.currentTimeMillis() - lastGcTime > 1000) { if (System.currentTimeMillis() - lastGcTime > 1000) {
System.gc(); // Triggern des Garbage Collectors System.gc(); // Triggern des Garbage Collectors
@ -80,26 +79,29 @@ public class CamPuffer implements Runnable {
} }
} finally { } finally {
// Matrizen explizit freigeben // Matrizen explizit freigeben
frame.release(); // Falls `frame` in der Schleife freigegeben wurde, hier nochmal frame.release();
capture.release(); // Kamera-Ressourcen freigeben capture.release();
} }
} }
private void updateCurrentMat(Mat img) { public void updateCurrentMat(Mat img) {
// Puffergröße überwachen Mat frameCopy = img.clone();
synchronized (frameBuffer) {
if (frameBuffer.size() >= MAX_BUFFER_SIZE) { if (frameBuffer.size() >= MAX_BUFFER_SIZE) {
Mat oldFrame = frameBuffer.poll(); // Entfernt das älteste Bild im Puffer frameBuffer.poll().release(); // Ältesten Frame entfernen
if (oldFrame != null) {
oldFrame.release(); // Verhindert das Anhäufen von nicht freigegebenen Mat-Objekten
} }
// Neues Bild in den Puffer hinzufügen frameBuffer.add(frameCopy); // Neuen Frame hinzufügen
frameBuffer.add(img.clone()); currentMat = frameCopy;
currentMat = img;
} }
} }
public Mat getCurrentMat() { public Mat getCurrentMat() {
return currentMat; synchronized (frameBuffer) {
if (!frameBuffer.isEmpty()) {
return frameBuffer.pollLast().clone(); // Rückgabe des neuesten Frames
}
}
return new Mat(); // Rückgabe eines leeren Frames, falls der Puffer leer ist
} }

View File

@ -6,7 +6,13 @@ import java.util.List;
public class GameLoop { public class GameLoop {
int activePlayer = 0; int activePlayer = 0;
private final int CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams)
private final int EXP = -9; // Belichtungszeit (idealerweise: 0)
private final int FPS = 20; // FPS
private final int THV = 245; // Threshold
ImgAnalyzer tracker; ImgAnalyzer tracker;
CamPuffer camPuffer;
GraphicsProgram graphics; GraphicsProgram graphics;
@ -19,8 +25,11 @@ public class GameLoop {
player1 = new Player(0, "192.168.1.1", 9000); player1 = new Player(0, "192.168.1.1", 9000);
player2 = new Player(1, "192.168.1.2", 9000); player2 = new Player(1, "192.168.1.2", 9000);
tracker = new ImgAnalyzer(); camPuffer = new CamPuffer(CAM, EXP, FPS, THV);
// Starte Tracker in einem separaten Thread Thread pufferThread = new Thread(camPuffer);
pufferThread.start();
tracker = new ImgAnalyzer(camPuffer, THV);
Thread trackerThread = new Thread(tracker::run); Thread trackerThread = new Thread(tracker::run);
trackerThread.start(); trackerThread.start();
@ -32,17 +41,12 @@ public class GameLoop {
gl = new GameLogic(); gl = new GameLogic();
String[] args_ = {"GameLogic"}; String[] args_ = {"GameLogic"};
PApplet.runSketch(args_, gl); */ PApplet.runSketch(args_, gl); */
try {
Thread.sleep(5000);
}catch (Exception e){
}
run(); run();
} }
private void run(){ private void run(){
/*while(true){ while(true){
//communicate(); //communicate();
Point p1 = tracker.getP1(); Point p1 = tracker.getP1();
@ -50,7 +54,7 @@ public class GameLoop {
Point p2 = tracker.getP2(); Point p2 = tracker.getP2();
//System.out.println("Player 2 is at " + p2.x + " and " + p2.y); //System.out.println("Player 2 is at " + p2.x + " and " + p2.y);
player1.setKoords(p1.x, p1.y); /*player1.setKoords(p1.x, p1.y);
player2.setKoords(p2.x, p2.y); player2.setKoords(p2.x, p2.y);
if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2 if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2
@ -61,10 +65,10 @@ public class GameLoop {
if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1 if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1
System.out.println("Spieler 2 hat verloren"); System.out.println("Spieler 2 hat verloren");
break; break;
}
draw();
} */ } */
//draw();
}
} }
private void communicate() { private void communicate() {

View File

@ -11,50 +11,56 @@ import java.awt.Point;
public class ImgAnalyzer { public class ImgAnalyzer {
private final int CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams) private final int THV; // Threshold
private final int EXP = -9; // Belichtungszeit (idealerweise: 0)
private final int FPS = 20; // FPS
private final int THV = 245; // Threshold
private Point currentPositionP1 = new Point(0, 0); private Point currentPositionP1 = new Point(0, 0);
private Point currentPositionP2 = new Point(0, 0); private Point currentPositionP2 = new Point(0, 0);
private CamPuffer camPuffer = new CamPuffer(CAM, EXP, FPS, THV);; private final CamPuffer camPuffer;
public ImgAnalyzer() { public ImgAnalyzer(CamPuffer cP, int threshold) {
Thread pufferThread = new Thread(camPuffer); THV = threshold;
pufferThread.start(); camPuffer = cP;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
} }
public static void main(String[] args) { public static void main(String[] args) {
ImgAnalyzer tracker = new ImgAnalyzer(); System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
tracker.run();
} }
public void run() { public void run() {
boolean hasStartedCapturing = false; while (camPuffer.getCurrentMat().elemSize() == 0) {
while (!hasStartedCapturing) { System.out.println("Warten auf das erste Bild...");
if (camPuffer.getCurrentMat() != null) { try {
break; Thread.sleep(3000); // Kurze Pause, um CPU-Belastung zu vermeiden
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
} }
} }
Mat currentImg = null; Mat currentImg = new Mat();
Mat grayFrame = new Mat(); Mat grayFrame = new Mat();
Mat thresholdFrame = new Mat(); Mat thresholdFrame = new Mat();
int debug = 0;
while (true) { while (true) {
try { try {
currentImg = camPuffer.getCurrentMat(); currentImg = camPuffer.getCurrentMat();
if(currentImg.elemSize() != 0) {
// In Graustufen konvertieren // In Graustufen konvertieren
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY);
// Threshold anwenden // Threshold anwenden
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY);
//Ausgabe des Bildes zum Prüfen
HighGui.imshow("ThunfischHighGui", thresholdFrame);
HighGui.waitKey(1);
//Ende Bildausgabe */
}
} catch (Exception e) { } catch (Exception e) {
} }