Skip to content

Commit

Permalink
Working code
Browse files Browse the repository at this point in the history
  • Loading branch information
willemk committed Jul 6, 2020
1 parent da299d3 commit e4c6aa5
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Changelog
*********


v0.1.0 (UNRELEASED)
v0.0.1 (UNRELEASED)
========================================

- Initial release.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Mopidy-QR to your Mopidy configuration file::
[qr]
# TODO: Add example of extension config

And run the following to add the proper user permissions:
```
sudo usermod -a -G video mopidy
```


Project resources
=================
Expand Down
75 changes: 75 additions & 0 deletions mopidy_qr/QrThread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from pyzbar import pyzbar
import logging
import threading
import time

from mopidy import core

logger = logging.getLogger(__name__)

class QRThread:
def __init__(self, config, core):
self.config = config
self.core = core
self._running = threading.Event()
self._thread = None
logging.debug("Initializing QRThread")


def start(self):
if self._thread is not None:
return
logging.debug("Starting QRThread")

self._running = threading.Event()
self._running.set()
self._thread = threading.Thread(target=self._loop)
self._thread.start()

def stop(self):
logging.debug("Stopping QRThread")

self._running.clear()
self._thread.join()
self._thread = None

def _loop(self):
logging.debug("Starting QR Reader")

import imutils.video

logger.debug("Import conpleted")

vs = imutils.video.VideoStream(usePiCamera=True).start()

time.sleep(5.0)

logger.debug("Initialization conpleted")

while self._running.is_set():
# grab the frame from the threaded video stream and resize it to
# have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)
# find the barcodes in the frame and decode each of the barcodes
barcodes = pyzbar.decode(frame)

if len(barcodes) == 0:
logger.debug("No barcodes found")

# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw
# the bounding box surrounding the barcode on the image

# the barcode data is a bytes object so if we want to draw it
# on our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
logger.info("[INFO] found barcode {} ({})".format(barcodeData, barcodeType))

self.core.tracklist.clear()
tracklist = self.core.tracklist.add(uris=[barcodeData]).get()

self.core.playback.play(tracklist[0])
25 changes: 9 additions & 16 deletions mopidy_qr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@ def get_config_schema(self):
def setup(self, registry):
# You will typically only implement one of the following things
# in a single extension.

# TODO: Edit or remove entirely
from .frontend import FoobarFrontend
registry.add("frontend", FoobarFrontend)

# TODO: Edit or remove entirely
from .backend import FoobarBackend
registry.add("backend", FoobarBackend)

# TODO: Edit or remove entirely
registry.add(
"http:static",
{
"name": self.ext_name,
"path": str(pathlib.Path(__file__).parent / "static"),
},
)
from .frontend import QR1Frontend
registry.add("frontend", QR1Frontend)
##

def start(self):
logger.info("Starting QR " + Extension.version)

def stop(self):
logger.info("Stopping QR")
36 changes: 36 additions & 0 deletions mopidy_qr/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging

import pykka
from mopidy import core

logger = logging.getLogger(__name__)


class QR1Frontend(pykka.ThreadingActor, core.CoreListener):

def __init__(self, config, core):
super().__init__()

self.core = core
self.config = config["qr"]

logging.info("Initializing QR Frontend")

from .QrThread import QRThread
self.QrThread = QRThread(self.config, self.core)

def on_start(self):
logging.info("Starting QR Frontend")

self.QrThread.start()


def on_stop(self):
logging.info("Stopping QR Frontend")
self.QrThread.stop()






105 changes: 105 additions & 0 deletions mopidy_qr/qr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# import the necessary packages
from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
import imutils
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", type=str, default="",
help="path to output CSV file containing barcodes")
args = vars(ap.parse_args())

# open the output CSV file for writing and initialize the set of
# barcodes found thus far
if args["input"] != "":
print("[INFO] using static image {}".format(args["input"]))

image = cv2.imread(args["input"])
barcodes = pyzbar.decode(image)

if len(barcodes) == 0:
print("[INFO] No barcodes found")

# loop over the detected barcodes
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
print("[INFO] found barcode {} ({})".format(barcodeData, barcodeType))
else:

# initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")

# vs = VideoStream(src=0).start()
vs = VideoStream(usePiCamera=True).start()
time.sleep(5.0)
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it to
# have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)
# find the barcodes in the frame and decode each of the barcodes
barcodes = pyzbar.decode(frame)

if len(barcodes) == 0:
print("[INFO] No barcodes found")

# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw
# the bounding box surrounding the barcode on the image

# the barcode data is a bytes object so if we want to draw it
# on our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
print("[INFO] found barcode {} ({})".format(barcodeData, barcodeType))

# close the output CSV file do a bit of cleanup
print("[INFO] cleaning up...")


# From # https://www.pyimagesearch.com/2018/05/21/an-opencv-barcode-and-qr-code-scanner-with-zbar/
# sudo apt-get install libzbar0
# pip3 install pyzbar
# pip3 install imutils

from imutils.video import VideoStream
from pyzbar import pyzbar
import imutils
import time

vs = VideoStream(usePiCamera=True).start()
time.sleep(5.0)

while True:
# grab the frame from the threaded video stream and resize it to
# have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)
# find the barcodes in the frame and decode each of the barcodes
barcodes = pyzbar.decode(frame)

if len(barcodes) == 0:
logging.log(logging.INFO, "No barcodes found")

# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw
# the bounding box surrounding the barcode on the image

# the barcode data is a bytes object so if we want to draw it
# on our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
logging.log(logging.DEBUG, "Found barcode {}({})".format(
barcodeData, barcodeType))

#core.tracklist.add()
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = Mopidy-QR
version = 0.1.0
version = 0.0.1
url = https://github.com/willemk/mopidy-qr
author = Willem Kappers
author_email =
Expand Down Expand Up @@ -28,7 +28,9 @@ install_requires =
Mopidy >= 3.0.0
Pykka >= 2.0.1
setuptools

pyzbar
imutils


[options.extras_require]
lint =
Expand Down

0 comments on commit e4c6aa5

Please sign in to comment.