+194
-40
lines changedFilter options
+194
-40
lines changed Original file line number Diff line number Diff line change
@@ -29,6 +29,12 @@
29
29
<artifactId>selenium-java</artifactId>
30
30
<version>${selenium.version}</version>
31
31
</dependency>
32
+
33
+
<dependency>
34
+
<groupId>org.slf4j</groupId>
35
+
<artifactId>slf4j-nop</artifactId>
36
+
<version>${slf4j.version}</version>
37
+
</dependency>
32
38
</dependencies>
33
39
34
40
<build>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package com.almasb.fxgl.intelligence;
8
+
9
+
/**
10
+
* Stores constants related to web-api projects.
11
+
* Changes to these values must be synchronized with the web-api project (https://github.com/AlmasB/web-api).
12
+
*
13
+
* @author Almas Baim (https://github.com/AlmasB)
14
+
*/
15
+
public final class WebAPI {
16
+
17
+
public static final String SPEECH_RECOGNITION_API = "https://almasb.github.io/web-api/speech-recog/";
18
+
public static final String GESTURE_RECOGNITION_API = "https://almasb.github.io/web-api/gesture-recog-v1/";
19
+
20
+
public static final int SPEECH_RECOGNITION_PORT = 55555;
21
+
public static final int GESTURE_RECOGNITION_PORT = 55560;
22
+
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package com.almasb.fxgl.speechrecog;
8
+
9
+
import com.almasb.fxgl.core.EngineService;
10
+
import com.almasb.fxgl.core.concurrent.Async;
11
+
import com.almasb.fxgl.logging.Logger;
12
+
import com.almasb.fxgl.ws.LocalWebSocketServer;
13
+
import org.openqa.selenium.WebDriver;
14
+
import org.openqa.selenium.chrome.ChromeDriver;
15
+
import org.openqa.selenium.chrome.ChromeOptions;
16
+
17
+
import java.util.function.Consumer;
18
+
19
+
import static com.almasb.fxgl.intelligence.WebAPI.SPEECH_RECOGNITION_API;
20
+
import static com.almasb.fxgl.intelligence.WebAPI.SPEECH_RECOGNITION_PORT;
21
+
22
+
public final class SpeechRecognitionService extends EngineService {
23
+
24
+
private static final Logger log = Logger.get(SpeechRecognitionService.class);
25
+
private LocalWebSocketServer server = new LocalWebSocketServer("SpeechRecogServer", SPEECH_RECOGNITION_PORT);
26
+
27
+
private WebDriver webDriver = null;
28
+
29
+
@Override
30
+
public void onInit() {
31
+
server.start();
32
+
}
33
+
34
+
/**
35
+
* Starts speech recognition in a background thread.
36
+
* Can be called after stop() to restart speech recognition.
37
+
* If the service has already started, then calls stop() and restarts it.
38
+
*/
39
+
public void start() {
40
+
Async.INSTANCE.startAsync(() -> {
41
+
try {
42
+
if (webDriver != null) {
43
+
stop();
44
+
}
45
+
46
+
ChromeOptions options = new ChromeOptions();
47
+
options.addArguments("--headless=new");
48
+
options.addArguments("--use-fake-ui-for-media-stream");
49
+
50
+
webDriver = new ChromeDriver(options);
51
+
webDriver.get(SPEECH_RECOGNITION_API);
52
+
53
+
// we are ready to use the web api service
54
+
55
+
} catch (Exception e) {
56
+
log.warning("Failed to start Chrome web driver. Ensure Chrome is installed in default location");
57
+
log.warning("Error data", e);
58
+
}
59
+
});
60
+
}
61
+
62
+
/**
63
+
* Stops speech recognition.
64
+
* No-op if it has not started via start() before.
65
+
*/
66
+
public void stop() {
67
+
try {
68
+
if (webDriver != null) {
69
+
webDriver.quit();
70
+
webDriver = null;
71
+
}
72
+
} catch (Exception e) {
73
+
log.warning("Failed to quit web driver", e);
74
+
}
75
+
}
76
+
77
+
/**
78
+
* Add a [handler] for incoming speech input.
79
+
*/
80
+
public void addInputHandler(Consumer<String> handler) {
81
+
server.addMessageHandler(handler);
82
+
}
83
+
84
+
/**
85
+
* Remove an existing input handler.
86
+
*/
87
+
public void removeInputHandler(Consumer<String> handler) {
88
+
server.removeMessageHandler(handler);
89
+
}
90
+
91
+
@Override
92
+
public void onExit() {
93
+
stop();
94
+
server.stop();
95
+
}
96
+
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public final class LocalWebSocketServer extends WebSocketServer {
32
32
33
33
private final Logger log;
34
34
35
-
private boolean isLoggingEnabled = true;
35
+
private boolean hasStarted = false;
36
36
37
37
private String serverName;
38
38
private List<Consumer<String>> messageHandlers = new ArrayList<>();
@@ -58,12 +58,8 @@ public LocalWebSocketServer(String serverName, int port) {
58
58
thread = new SendMessageThread(serverName);
59
59
}
60
60
61
-
public boolean isLoggingEnabled() {
62
-
return isLoggingEnabled;
63
-
}
64
-
65
-
public void setLoggingEnabled(boolean isLoggingEnabled) {
66
-
this.isLoggingEnabled = isLoggingEnabled;
61
+
public boolean hasStarted() {
62
+
return hasStarted;
67
63
}
68
64
69
65
/**
@@ -109,14 +105,14 @@ public boolean isConnected() {
109
105
110
106
@Override
111
107
public void onOpen(WebSocket conn, ClientHandshake handshake) {
112
-
log.info("Opened connection: " + conn.getRemoteSocketAddress());
108
+
log.debug("Opened connection: " + conn.getRemoteSocketAddress());
113
109
114
110
socket = conn;
115
111
}
116
112
117
113
@Override
118
114
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
119
-
log.info("Closed connection, code: " + code);
115
+
log.debug("Closed connection, code: " + code);
120
116
}
121
117
122
118
@Override
@@ -131,14 +127,16 @@ public void onError(WebSocket conn, Exception ex) {
131
127
132
128
@Override
133
129
public void onStart() {
134
-
log.info("Server started successfully");
130
+
log.debug("Server started successfully");
135
131
}
136
132
137
133
@Override
138
134
public void start() {
139
135
try {
140
136
thread.start();
141
137
super.start();
138
+
139
+
hasStarted = true;
142
140
} catch (Exception e) {
143
141
log.warning("Failed to start WS server", e);
144
142
}
Original file line number Diff line number Diff line change
@@ -27,6 +27,12 @@
27
27
<artifactId>fxgl-controllerinput</artifactId>
28
28
<version>${fxgl.version}</version>
29
29
</dependency>
30
+
31
+
<dependency>
32
+
<groupId>com.github.almasb</groupId>
33
+
<artifactId>fxgl-intelligence</artifactId>
34
+
<version>${fxgl.version}</version>
35
+
</dependency>
30
36
</dependencies>
31
37
32
38
<build>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
1
+
/*
2
+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3
+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4
+
* See LICENSE for details.
5
+
*/
6
+
7
+
package sandbox.net;
8
+
9
+
import com.almasb.fxgl.app.GameApplication;
10
+
import com.almasb.fxgl.app.GameSettings;
11
+
import com.almasb.fxgl.speechrecog.SpeechRecognitionService;
12
+
import com.almasb.fxgl.ui.FontType;
13
+
import javafx.scene.control.TextArea;
14
+
15
+
import static com.almasb.fxgl.dsl.FXGL.*;
16
+
17
+
/**
18
+
* Shows how to use the speech recognition service.
19
+
*
20
+
* @author Almas Baim (https://github.com/AlmasB)
21
+
*/
22
+
public class SpeechRecogSample extends GameApplication {
23
+
24
+
private TextArea output;
25
+
26
+
@Override
27
+
protected void initSettings(GameSettings settings) {
28
+
settings.addEngineService(SpeechRecognitionService.class);
29
+
}
30
+
31
+
@Override
32
+
protected void initGame() {
33
+
getService(SpeechRecognitionService.class).addInputHandler(input -> {
34
+
getExecutor().startAsyncFX(() -> {
35
+
output.appendText(input + "\n");
36
+
});
37
+
});
38
+
39
+
getService(SpeechRecognitionService.class).start();
40
+
}
41
+
42
+
@Override
43
+
protected void initUI() {
44
+
output = new TextArea();
45
+
output.setWrapText(true);
46
+
output.setPrefSize(getAppWidth(), getAppHeight());
47
+
output.setFont(getUIFactoryService().newFont(FontType.MONO, 18));
48
+
49
+
addUINode(output);
50
+
}
51
+
52
+
public static void main(String[] args) {
53
+
launch(args);
54
+
}
55
+
}
Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@
96
96
<jackson.version>2.14.2</jackson.version>
97
97
<websocket.version>1.5.5</websocket.version>
98
98
<selenium.version>4.17.0</selenium.version>
99
+
<slf4j.version>2.0.6</slf4j.version>
99
100
100
101
<!-- test dependencies -->
101
102
<junit.jupiter.version>5.10.0</junit.jupiter.version>
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4