Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Connect Four Prototype
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KI-gestütztes Marketing
Prototyping
Connect Four Prototype
Commits
842a20cc
Commit
842a20cc
authored
Apr 30, 2024
by
Simon Flaisch
Browse files
Options
Downloads
Patches
Plain Diff
Changed the game to work with our architecture. For that I implemented mqtt clients and a Server.js
parent
120ed394
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Server.js
+31
-0
31 additions, 0 deletions
Server.js
app.js
+6
-1
6 additions, 1 deletion
app.js
index.html
+1
-1
1 addition, 1 deletion
index.html
main.py
+94
-1
94 additions, 1 deletion
main.py
with
132 additions
and
3 deletions
Server.js
0 → 100644
+
31
−
0
View file @
842a20cc
const
mqtt
=
require
(
"
mqtt
"
);
const
ws
=
require
(
"
ws
"
);
let
socketserver
=
new
ws
.
WebSocketServer
({
port
:
443
});
let
client
=
mqtt
.
connect
(
"
mqtt://localhost:1883
"
,{
username
:
"
standardUser
"
,
password
:
"
GreatHHZ4Ever!
"
});
client
.
on
(
"
connect
"
,
function
()
{
let
topicID
=
"
Topic1
"
;
client
.
subscribe
(
topicID
);
});
socketserver
.
on
(
'
connection
'
,
ws
=>
{
console
.
log
(
"
new client connected
"
);
});
socketserver
.
on
(
'
close
'
,
()
=>
console
.
log
(
'
Client has disconnected!
'
));
client
.
on
(
"
message
"
,
function
(
topic
,
message
){
data
=
JSON
.
parse
(
message
.
toString
())
var
column
=
{
column
:
Math
.
min
(
data
.
total_fingers
,
6
)}
socketserver
.
clients
.
forEach
(
client
=>
{
client
.
send
(
JSON
.
stringify
(
column
));
})});
This diff is collapsed.
Click to expand it.
app.js
+
6
−
1
View file @
842a20cc
document
.
addEventListener
(
'
DOMContentLoaded
'
,
()
=>
{
const
flaechen
=
document
.
querySelectorAll
(
'
.grid div
'
);
const
ergebnis
=
document
.
querySelector
(
'
#result
'
);
...
...
@@ -5,6 +6,7 @@ document.addEventListener('DOMContentLoaded', () => {
let
currentPlayer
=
1
;
const
gewinnFlaechen
=
[
[
0
,
1
,
2
,
3
],
[
41
,
40
,
39
,
38
],
...
...
@@ -77,7 +79,8 @@ document.addEventListener('DOMContentLoaded', () => {
[
13
,
20
,
27
,
34
],
]
// Verbinden mit dem WebSocket.
const
ws
=
new
WebSocket
(
'
ws://localhost:89/ws
'
);
const
ws
=
new
WebSocket
(
'
ws://localhost:443/ws
'
);
ws
.
onmessage
=
function
(
event
)
{
const
data
=
JSON
.
parse
(
event
.
data
);
...
...
@@ -155,3 +158,5 @@ function placeChipInColumn(column) {
}
})
This diff is collapsed.
Click to expand it.
index.html
+
1
−
1
View file @
842a20cc
...
...
@@ -5,7 +5,7 @@
<meta
charset=
"utf-8"
>
<title>
Connect Four
</title>
<link
rel=
"stylesheet"
href=
"style.css"
>
<script
src=
"app.js"
charset=
"utf-8"
defer
></script>
<script
type=
"module"
src=
"app.js"
charset=
"utf-8"
defer
></script>
</head>
<body>
<h1>
Der derzeitige Spieler ist: Spieler
<span
id=
"current-player"
>
1
</span></h1>
...
...
This diff is collapsed.
Click to expand it.
main.py
+
94
−
1
View file @
842a20cc
import
json
import
uvicorn
from
fastapi
import
FastAPI
,
WebSocket
from
fastapi.responses
import
HTMLResponse
,
Response
import
cv2
from
cvzone.HandTrackingModule
import
HandDetector
from
imutils.video
import
VideoStream
import
paho.mqtt.client
as
mqtt
import
asyncio
import
time
unacked_publish
=
set
()
main_time
=
0
response
=
None
def
on_connect
(
client
,
userdata
,
flags
,
reason_code
,
properties
):
print
(
f
"
Connected with result code
{
reason_code
}
\n
"
)
client
.
subscribe
(
"
Topic1
"
)
# Funktion, die bei einem Publish in ein Topic ausgeführt wird.
def
on_publish
(
client
,
userdata
,
mid
,
reason_code
,
properties
):
try
:
userdata
.
remove
(
mid
)
# Removed die gesendete Nachricht vom Speicher, um eine doppelte Sendung zu verhindern
except
KeyError
:
"
Something went wrong. Please check if you have removed the mid from the userdata
"
# Funktion zur Verbindung mit MQTT
def
connectwithmqtt
(
adress
:
str
,
targetport
:
int
):
mqttc
=
mqtt
.
Client
(
mqtt
.
CallbackAPIVersion
.
VERSION2
,
userdata
=
None
)
# Intitalisierung des Clients
mqttc
.
on_connect
=
on_connect
mqttc
.
on_publish
=
on_publish
mqttc
.
user_data_set
(
unacked_publish
)
# Den lokalen Speicher als User data setzen
mqttc
.
username_pw_set
(
username
=
"
standardUser
"
,
password
=
"
GreatHHZ4Ever!
"
)
# Username und Passwort - vorgegeben vom MQTT Server
mqttc
.
connect
(
host
=
adress
,
port
=
targetport
)
while
not
mqttc
.
is_connected
():
mqttc
.
loop
()
print
(
"
Waiting for connection...
"
)
return
mqttc
# Funktion zum senden einer Nachricht
def
sendMessage
(
mqttc
,
topic
,
message
):
mqttc
.
loop_start
()
# Started einen gethreaded connection loop zum senden der Nachrichten.
msg
=
mqttc
.
publish
(
topic
,
message
,
qos
=
1
)
unacked_publish
.
add
(
msg
.
mid
)
# Mid sind die Daten der aktuellen Nachricht
while
len
(
unacked_publish
):
time
.
sleep
(
0.1
)
# Wartet bis alle gequeten Nachrichten gepublished werden
msg
.
wait_for_publish
()
mqttClient
=
connectwithmqtt
(
adress
=
"
localhost
"
,
targetport
=
1883
)
app
=
FastAPI
()
# Video-Capture initialisieren
try
:
video_capture
=
VideoStream
(
"
http://localhost:8
8
/video_feed
"
).
start
()
video_capture
=
VideoStream
(
"
http://localhost:8
0
/video_feed
"
).
start
()
except
Exception
as
e
:
print
(
f
"
We couldn
'
t reach the camera
"
)
exit
()
...
...
@@ -19,7 +67,42 @@ except Exception as e:
# Hand-Detektor initialisieren
detector
=
HandDetector
(
maxHands
=
2
)
while
True
:
frame
=
video_capture
.
read
()
if
frame
is
None
:
print
(
"
Sorry, couldn
'
t receive videostream
"
)
exit
()
# Handgesten-Erkennung
hands
,
img
=
detector
.
findHands
(
frame
)
current_time
=
time
.
time
()
# Aktueller Zeitpunkt
if
current_time
-
main_time
>
5
:
# Timer
main_time
=
current_time
if
hands
:
# Zähle die Anzahl der gestreckten Finger
total_fingers
=
0
for
hand
in
hands
:
fingers
=
detector
.
fingersUp
(
hand
)
total_fingers
+=
sum
(
fingers
)
response
=
{
"
fingers
"
:
fingers
,
"
total_fingers
"
:
total_fingers
}
if
response
is
not
None
:
sendMessage
(
mqttc
=
mqttClient
,
topic
=
"
Topic1
"
,
message
=
json
.
dumps
(
response
))
# Bild fürs Debugging anzeigen
cv2
.
imshow
(
"
Video
"
,
img
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'
q
'
):
print
(
"
Goodbye!
"
)
mqttClient
.
disconnect
()
mqttClient
.
loop_stop
()
break
video_capture
.
stop
()
cv2
.
destroyAllWindows
()
'''
@app.websocket(
"
/ws
"
)
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
...
...
@@ -83,3 +166,13 @@ async def get_image():
if __name__ ==
"
__main__
"
:
uvicorn.run(app, host=
"
localhost
"
, port=89)
# Begrenze die Fingeranzahl auf 7, da wir 7 Spalten haben (0-6)
selected_column = min(total_fingers, 6)
print(
"
Selected column:
"
, selected_column)
try:
await websocket.send_json({
"
column
"
: selected_column})
except Exception as e:
print(f
"
Error sending message: {str(e)}
"
)
'''
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment