Skip to content

Commit

Permalink
added threading for processing the blurriness value
Browse files Browse the repository at this point in the history
  • Loading branch information
tobykurien committed Jan 7, 2016
1 parent 5c88419 commit f365047
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
27 changes: 27 additions & 0 deletions app/modules/focus.py
@@ -0,0 +1,27 @@
import cv2
from threading import Thread
from Queue import Queue

class ProcessFocus(Thread):
"""Work out a focus value for a given frame"""

def __init__(self):
Thread.__init__(self)
self.queue = Queue()
self.focus = -1
self.setDaemon(True) # terminate on exit

def variance_of_laplacian(self, image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(image, cv2.CV_64F).var()

def run(self):
frame = None
while True:
try:
frame = self.queue.get()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
self.focus = self.variance_of_laplacian(gray)
except:
print("ERROR in ProcessFocus")
22 changes: 11 additions & 11 deletions app/piscope.py
@@ -1,8 +1,8 @@
import cv2
import yaml
import codecs

from modules.camera import Camera
from modules.focus import ProcessFocus

def read_config(filepath):
# read configuration file
Expand All @@ -14,27 +14,27 @@ def read_config(filepath):

return config

def variance_of_laplacian(image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(image, cv2.CV_64F).var()

if __name__ == "__main__":
config = read_config('config.yml')

focus = ProcessFocus()
focus.start()

cam = Camera(rpiCam=config['rpi_camera'], cameraNum=config['camera_number'])
cv2.namedWindow("Image", flags=cv2.CV_WINDOW_AUTOSIZE)
while True:
image = cam.grabFrame()
image = cam.grabFrame()

# detect focus
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
if focus.queue.qsize() == 0:
focus.queue.put(image)

text = "Not Blurry"
if fm < 100:
if focus.focus < 100:
text = "Blurry"

# show the image
cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
cv2.putText(image, "{}: {:.2f}".format(text, focus.focus), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
cv2.imshow("Image", image)

Expand Down

0 comments on commit f365047

Please sign in to comment.