This is my first stack overflow question. Basically, I'm creating a QR code reader that will read a URL on a QR code and automatically open a chrome browser to that link.
The easiest way I've found to do this and allow it to keep running is for the app to keep looping and restarting itself.
Here's my code
#!/usr/bin/env python
import cv2
import urllib.request
import webbrowser
import time
import os
# open a connection to a URL using urllib
webUrl = urllib.request.urlopen
# set up camera object
cap = cv2.VideoCapture(0)
# QR code detection object
detector = cv2.QRCodeDetector()
data=""
triggerTime=0
while True:
# get the image
_, img = cap.read()
# get bounding box coords and data
data, bbox, _ = detector.detectAndDecode(img)
# if there is a bounding box, draw one, along with the data
if(bbox is not None):
for i in range(len(bbox)):
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
0, 255), thickness=2)
cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
if data:
url = data
chrome_path = '/usr/bin/chromium-browser'
webbrowser.get(chrome_path).open(url)
print("data found: ", data)
while 1:
os.system("/home/pi/Desktop/cameratest.py")
print("Restarting Script")
exit()
# display the image preview
cv2.imshow("QR Parser", img)
if(cv2.waitKey(1) == ord("q")):
break
# free camera object and exit
cap.release()
cv2.destroyAllWindows()
So my code runs fine, it reads the QR code, recognizes it and then goes to the URL using Chromium. Once the code loops and attempts to restart itself, it opens the script and then gives "ImportError: No module named cv2"
Any help would be greatly appreciated. I have no clue why this isn't working.
I have opencv2-python installed and I'm running raspbian.