This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b406c5
Showing
16 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/> | ||
<classpathentry kind="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>FXHelloCV</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.7 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="ASCII"?> | ||
<anttasks:AntTask xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:anttasks="http://org.eclipse.fx.ide.jdt/1.0" buildDirectory="${project}/build" cssToBin="true"> | ||
<deploy packagingFormat="image"> | ||
<application name="FXHelloCV" mainclass="it.polito.elite.teaching.cv.FXHelloCV" toolkit="fx"/> | ||
<info title="FXHelloCV"/> | ||
</deploy> | ||
<signjar/> | ||
<files key="opencv" value="D:\Applications\opencv\build\java\x64\opencv_java246.dll"/> | ||
</anttasks:AntTask> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?import javafx.geometry.*?> | ||
<?import javafx.scene.control.*?> | ||
<?import javafx.scene.layout.*?> | ||
<?import javafx.scene.image.*?> | ||
|
||
|
||
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="it.polito.elite.teaching.cv.FXHelloCVController"> | ||
<center> | ||
<ImageView id="currentFrame" /> | ||
</center> | ||
<bottom> | ||
<HBox alignment="center" > | ||
<padding> | ||
<Insets top="25" right="25" bottom="25" left="25"/> | ||
</padding> | ||
<Button fx:id="button" alignment="center" text="Start camera" onAction="#startCamera" /> | ||
</HBox> | ||
</bottom> | ||
</BorderPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package it.polito.elite.teaching.cv; | ||
|
||
import org.opencv.core.Core; | ||
|
||
import javafx.application.Application; | ||
import javafx.stage.Stage; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.BorderPane; | ||
import javafx.fxml.FXMLLoader; | ||
|
||
/** | ||
* The main class for a JavaFX application. It creates and handle the main | ||
* window with its resources (style, graphics, etc.). | ||
* | ||
* @author <a href="mailto:[email protected]">Luigi De Russis</a> | ||
* @since 2013-10-20 | ||
* | ||
*/ | ||
public class FXHelloCV extends Application | ||
{ | ||
// the root element in the FXML window | ||
private BorderPane rootElement; | ||
|
||
@Override | ||
public void start(Stage primaryStage) | ||
{ | ||
try | ||
{ | ||
// load the FXML resource | ||
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXHelloCV.fxml")); | ||
// store the root element so that the controllers can use it | ||
this.rootElement = (BorderPane) loader.load(); | ||
// create and style a scene | ||
Scene scene = new Scene(this.rootElement, 800, 600); | ||
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); | ||
// create the stage with the given title and the previously created | ||
// scene | ||
primaryStage.setTitle("JavaFX meets OpenCV"); | ||
primaryStage.setScene(scene); | ||
// show the GUI | ||
primaryStage.show(); | ||
|
||
// set a reference of this class for its controller | ||
FXHelloCVController controller = loader.getController(); | ||
controller.setMainApp(this); | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Getter for the rootElement | ||
* | ||
* @return the {@link BorderPane} that represents the root element | ||
*/ | ||
public BorderPane getRootElement() | ||
{ | ||
return this.rootElement; | ||
} | ||
|
||
/** | ||
* For launching the application... | ||
* | ||
* @param args | ||
* optional params | ||
*/ | ||
public static void main(String[] args) | ||
{ | ||
// load the native OpenCV library | ||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | ||
|
||
launch(args); | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
FXHelloCV/src/it/polito/elite/teaching/cv/FXHelloCVController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package it.polito.elite.teaching.cv; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
import javafx.beans.property.ObjectProperty; | ||
import javafx.beans.property.SimpleObjectProperty; | ||
import javafx.event.ActionEvent; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.image.Image; | ||
import javafx.scene.image.ImageView; | ||
|
||
import org.opencv.core.Mat; | ||
import org.opencv.core.MatOfByte; | ||
import org.opencv.highgui.Highgui; | ||
import org.opencv.highgui.VideoCapture; | ||
import org.opencv.imgproc.Imgproc; | ||
|
||
/** | ||
* The controller for our application, where the application logic is | ||
* implemented. It handles the button for starting/stopping the camera and the | ||
* acquired video stream. | ||
* | ||
* @author <a href="mailto:[email protected]">Luigi De Russis</a> | ||
* @since 2013-10-20 | ||
* | ||
*/ | ||
public class FXHelloCVController | ||
{ | ||
// the FXML button | ||
@FXML | ||
private Button button; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
luigidr
Author
Member
|
||
|
||
// the main app | ||
private FXHelloCV mainApp; | ||
// a timer for acquiring the video stream | ||
private Timer timer; | ||
// the OpenCV object that realizes the video capture | ||
private VideoCapture capture = new VideoCapture(); | ||
// a flag to change the button behavior | ||
private boolean cameraActive = false; | ||
|
||
/** | ||
* The action triggered by pushing the button on the GUI | ||
* | ||
* @param event | ||
* the push button event | ||
*/ | ||
@FXML | ||
protected void startCamera(ActionEvent event) | ||
{ | ||
// check: the main class is accessible? | ||
if (this.mainApp != null) | ||
{ | ||
// get the ImageView object for showing the video stream | ||
final ImageView frameView = (ImageView) mainApp.getRootElement().lookup("#currentFrame"); | ||
// bind an image property with the container for frames | ||
final ObjectProperty<Image> imageProp = new SimpleObjectProperty<>(); | ||
frameView.imageProperty().bind(imageProp); | ||
|
||
if (!this.cameraActive) | ||
{ | ||
// start the video capture | ||
this.capture.open(0); | ||
|
||
// is the video stream available? | ||
if (this.capture.isOpened()) | ||
{ | ||
this.cameraActive = true; | ||
|
||
// grab a frame every 33 ms (30 frames/sec) | ||
TimerTask frameGrabber = new TimerTask() { | ||
@Override | ||
public void run() | ||
{ | ||
// update the image property => update the frame | ||
// shown in the UI | ||
imageProp.set(grabFrame()); | ||
} | ||
}; | ||
this.timer = new Timer(); | ||
this.timer.schedule(frameGrabber, 0, 33); | ||
|
||
// update the button content | ||
this.button.setText("Stop Camera"); | ||
} | ||
else | ||
{ | ||
// log the error | ||
System.err.println("Impossible to open the camera connection..."); | ||
} | ||
} | ||
else | ||
{ | ||
// the camera is not active at this point | ||
this.cameraActive = false; | ||
// update again the button content | ||
this.button.setText("Start Camera"); | ||
// stop the timer | ||
if (this.timer != null) | ||
{ | ||
this.timer.cancel(); | ||
this.timer = null; | ||
} | ||
// release the camera | ||
this.capture.release(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get a frame from the opened video stream (if any) | ||
* | ||
* @return the {@link Image} to show | ||
*/ | ||
private Image grabFrame() | ||
{ | ||
// init everything | ||
Image imageToShow = null; | ||
Mat frame = new Mat(); | ||
|
||
// check if the capture is open | ||
if (this.capture.isOpened()) | ||
{ | ||
try | ||
{ | ||
// read the current frame | ||
this.capture.read(frame); | ||
|
||
// if the frame is not empty, process it | ||
if (!frame.empty()) | ||
{ | ||
// convert the image to gray scale | ||
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY); | ||
// convert the Mat object (OpenCV) to Image (JavaFX) | ||
imageToShow = mat2Image(frame); | ||
} | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
// log the error | ||
System.err.println("ERROR: " + e.getMessage()); | ||
} | ||
} | ||
|
||
return imageToShow; | ||
} | ||
|
||
/** | ||
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX | ||
* | ||
* @param frame | ||
* the {@link Mat} representing the current frame | ||
* @return the {@link Image} to show | ||
*/ | ||
private Image mat2Image(Mat frame) | ||
{ | ||
// create a temporary buffer | ||
MatOfByte buffer = new MatOfByte(); | ||
// encode the frame in the buffer | ||
Highgui.imencode(".png", frame, buffer); | ||
// build and return an Image created from the image encoded in the | ||
// buffer | ||
return new Image(new ByteArrayInputStream(buffer.toArray())); | ||
} | ||
|
||
/** | ||
* Set the reference to the main class of the application | ||
* | ||
* @param mainApp | ||
* the {@FXHelloCV} object to set | ||
*/ | ||
public void setMainApp(FXHelloCV mainApp) | ||
{ | ||
this.mainApp = mainApp; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* intentionally left empty */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv2"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>HelloCV</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.7 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.7 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
According to the tutorial available here the line should be as following:
private Button start_btn
and further appearance of 'button' should be replaced with 'start_btn' accordingly. Otherwise the code works but the camera is not released after it's started and some parts of the code don't work as intended (for instance, changing the text of the button)