Bild Übergabe Check Noch offen: Merge mit Bildanalyse und Zeichnen 16.12.2024 21:35master
<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 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"> | |||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> | |||||
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48"> | |||||
<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" /> | ||||
</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"> | ||||
<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" /> | ||||
<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 Bild wird nicht korrekt gespeichert 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"> | ||||
<MESSAGE value="Version 27.11" /> | <MESSAGE value="Version 27.11" /> | ||||
<MESSAGE value="Version 27.11 ("fertiger" Tracker)" /> | <MESSAGE value="Version 27.11 ("fertiger" Tracker)" /> | ||||
<MESSAGE value="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> | <MESSAGE value="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> | ||||
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer 16.12.2024 15.06 Siuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu" /> | |||||
<MESSAGE value="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48" /> | |||||
<option name="LAST_COMMIT_MESSAGE" value="Funktionierender Puffer Bild wird nicht korrekt gespeichert 16.12.2024 16:48" /> | |||||
</component> | </component> | ||||
</project> | </project> |
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; | ||||
} | } | ||||
Mat frame = new Mat(); | Mat frame = new Mat(); | ||||
capture.read(frame); | |||||
hasStartedCapturing = true; | hasStartedCapturing = true; | ||||
long lastGcTime = System.currentTimeMillis(); | long lastGcTime = System.currentTimeMillis(); | ||||
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 | ||||
} | } | ||||
} finally { | } finally { | ||||
// Matrizen explizit freigeben | // Matrizen explizit freigeben | ||||
frame.release(); // Falls `frame` in der Schleife freigegeben wurde, hier nochmal | |||||
capture.release(); // Kamera-Ressourcen freigeben | |||||
frame.release(); | |||||
capture.release(); | |||||
} | } | ||||
} | } | ||||
private void updateCurrentMat(Mat img) { | |||||
// Puffergröße überwachen | |||||
if (frameBuffer.size() >= MAX_BUFFER_SIZE) { | |||||
Mat oldFrame = frameBuffer.poll(); // Entfernt das älteste Bild im Puffer | |||||
if (oldFrame != null) { | |||||
oldFrame.release(); // Verhindert das Anhäufen von nicht freigegebenen Mat-Objekten | |||||
public void updateCurrentMat(Mat img) { | |||||
Mat frameCopy = img.clone(); | |||||
synchronized (frameBuffer) { | |||||
if (frameBuffer.size() >= MAX_BUFFER_SIZE) { | |||||
frameBuffer.poll().release(); // Ältesten Frame entfernen | |||||
} | } | ||||
// Neues Bild in den Puffer hinzufügen | |||||
frameBuffer.add(img.clone()); | |||||
currentMat = img; | |||||
frameBuffer.add(frameCopy); // Neuen Frame hinzufügen | |||||
currentMat = frameCopy; | |||||
} | } | ||||
} | } | ||||
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 | |||||
} | } | ||||
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; | ||||
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(); | |||||
// Starte Tracker in einem separaten Thread | |||||
camPuffer = new CamPuffer(CAM, EXP, FPS, THV); | |||||
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(); | ||||
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(); | ||||
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 | ||||
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() { |
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 EXP = -9; // Belichtungszeit (idealerweise: 0) | |||||
private final int FPS = 20; // FPS | |||||
private final int THV = 245; // Threshold | |||||
private final int THV; // 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() { | |||||
Thread pufferThread = new Thread(camPuffer); | |||||
pufferThread.start(); | |||||
public ImgAnalyzer(CamPuffer cP, int threshold) { | |||||
THV = threshold; | |||||
camPuffer = cP; | |||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |||||
} | } | ||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
ImgAnalyzer tracker = new ImgAnalyzer(); | |||||
tracker.run(); | |||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |||||
} | } | ||||
public void run() { | public void run() { | ||||
boolean hasStartedCapturing = false; | |||||
while (!hasStartedCapturing) { | |||||
if (camPuffer.getCurrentMat() != null) { | |||||
break; | |||||
while (camPuffer.getCurrentMat().elemSize() == 0) { | |||||
System.out.println("Warten auf das erste Bild..."); | |||||
try { | |||||
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 { | |||||
currentImg = camPuffer.getCurrentMat(); | |||||
try { | |||||
currentImg = camPuffer.getCurrentMat(); | |||||
// In Graustufen konvertieren | |||||
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); | |||||
if(currentImg.elemSize() != 0) { | |||||
// Threshold anwenden | |||||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); | |||||
// In Graustufen konvertieren | |||||
Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); | |||||
} catch (Exception e) { | |||||
// Threshold anwenden | |||||
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); | |||||
} | |||||
//Ausgabe des Bildes zum Prüfen | |||||
HighGui.imshow("ThunfischHighGui", thresholdFrame); | |||||
HighGui.waitKey(1); | |||||
//Ende Bildausgabe */ | |||||
} | |||||
if (currentImg != null) { | |||||
List<Point> positions = trackPositions(thresholdFrame); | |||||
} catch (Exception e) { | |||||
if (positions.size() >= 2) { | |||||
currentPositionP1 = positions.get(0); | |||||
currentPositionP2 = positions.get(1); | |||||
System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); | |||||
} | } | ||||
} | |||||
if (currentImg != null) { | |||||
List<Point> positions = trackPositions(thresholdFrame); | |||||
if (positions.size() >= 2) { | |||||
currentPositionP1 = positions.get(0); | |||||
currentPositionP2 = positions.get(1); | |||||
System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||