Skip to content

Commit

Permalink
Minor cleanup and a new webdemo
Browse files Browse the repository at this point in the history
  • Loading branch information
hugs committed Jun 22, 2023
1 parent f67a900 commit 304b98d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
6 changes: 3 additions & 3 deletions examples/demo.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import mvb

proc main() =
echo "Running MVB (🖥️ 👁️ 📒) Demo..."
var frame: Mat

# Create a GUI window
namedWindow "MVB - OpenCV Demo"
#namedWindow "MVB - OpenCV Demo"

# Open the camera by callling the constructor
let cap = newVideoCapture()
Expand All @@ -15,7 +16,7 @@ proc main() =
# Start getting and showing frames
while true:
# Grab frame from camera
cap.read(frame)
cap.read frame

# Display the frame
imshow "MVB - OpenCV Demo", frame
Expand All @@ -28,6 +29,5 @@ proc main() =
cap.release
echo "👑"


when isMainModule:
main()
53 changes: 53 additions & 0 deletions examples/webdemo.nim
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()
8 changes: 7 additions & 1 deletion mvb.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ backend = "cpp"
requires "nim >= 1.6.12"
requires "cppstl"

task i, "\t\tRun INim interpreter configured to use MVB":
exec "inim -d:--backend:cpp -d:'--passL:\"-lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio\"'"

task demo, "\t\tRun the demo":
exec "nim cpp -r examples/demo.nim"
exec "nim cpp -r --hints:off examples/demo.nim"

task web_demo, "\t\tRun the web demo":
exec "nim cpp -r --hints:off examples/webdemo.nim"

task test, "\t\tRun the test suite":
exec "nim cpp -r --hints:off tests/test1.nim"
Expand Down
9 changes: 6 additions & 3 deletions src/mvb.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cppstl/std_string
import cppstl/std_string, cppstl/std_vector

#{.passl: "-lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio".}

Expand Down Expand Up @@ -78,12 +78,15 @@ const
CAP_REALSENSE* = CAP_INTELPERC ## !< Synonym for CAP_INTELPERC

# imgcodecs
proc imread*(file: cstring, mode: ImreadModes = IMREAD_COLOR): Mat
proc imread*(filename: cstring, mode: ImreadModes = IMREAD_COLOR): Mat
{.importcpp: "cv::imread(@)", header: imgcodecs.}

proc imwrite*(file: cstring, mat : Mat): bool
proc imwrite*(filename: cstring, img : Mat): bool
{.importcpp: "cv::imwrite(@)", header: imgcodecs, discardable.}

proc imencode*(ext: cstring, img: Mat, buf: CppVector[uint8]): bool
{.importcpp: "cv::imencode(@)", header: imgcodecs, discardable.}

# highgui
proc namedWindow*(winname: cstring, flags : WindowFlags = WINDOW_AUTOSIZE)
{.importcpp: "cv::namedWindow(@)", header: highgui, discardable.}
Expand Down
2 changes: 1 addition & 1 deletion src/mvb.nim.cfg
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"

0 comments on commit 304b98d

Please sign in to comment.