-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
70 additions
and
8 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
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,53 @@ | ||
# To run: | ||
# $ nim cpp -r web3.nim | ||
# | ||
import asynchttpserver, asyncdispatch, asyncnet | ||
from std/posix import SIGINT, SIGTERM, onSignal | ||
import cppstl/std_vector | ||
import mvb | ||
|
||
const address = "127.0.0.1" | ||
const port = 8080 | ||
|
||
# Camera set-up | ||
let cap = newVideoCapture() | ||
var frame: Mat | ||
|
||
# Server set-up | ||
const headers = {"Content-Type": "text/html; charset=utf-8"} | ||
|
||
# Get a frame from OpenCV: where the real magic happens... | ||
proc getFrame(): string = | ||
cap.read(frame) | ||
var buffer = initCppVector[uint8]() | ||
var frameStr: string | ||
imencode(".jpg", frame, buffer) | ||
if buffer.len > 0: | ||
frameStr = newString(buffer.len) | ||
copyMem(frameStr.cstring, buffer[0].unsafeAddr, buffer.len) | ||
else: | ||
frameStr = newString(0) | ||
result = frameStr | ||
|
||
proc cb(req: Request) {.async.} = | ||
echo req.reqMethod, " ", req.url.path | ||
if req.url.path == "/": | ||
const response = "<h3>🖥️👁️📒 / mvb - web demo</h3>\n" & | ||
"<img src=\"/video\" width=\"100%\">" | ||
await req.respond(Http200, response, headers.newHttpHeaders()) | ||
|
||
elif req.url.path == "/video": | ||
await req.client.send("HTTP/1.1 200 OK\r\n" & | ||
"Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n") | ||
while true: | ||
await req.client.send("--frame\r\nContent-Type: image/jpeg\r\n\r\n" & getFrame() & "\r\n") | ||
else: | ||
await req.respond(Http404, "<h1 style='font-size: 200;'>🤷</h1>", headers.newHttpHeaders()) | ||
|
||
onSignal(SIGINT, SIGTERM): | ||
echo "⏹️ Stopping..." | ||
quit() | ||
|
||
echo "▶️ Starting server at http://" & address & ":" & $port & "/" | ||
asyncCheck newAsyncHttpServer().serve(Port(port), cb, address) | ||
runForever() |
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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
--passL:"-lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio" | ||
#--passL:"-lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio" |