Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
vallettea committed Nov 4, 2014
1 parent ea9936d commit 6f74c4c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
59 changes: 28 additions & 31 deletions check.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@


import logging
import glob
import re
import os
import sys
from subprocess import call
from subprocess import check_call

logText = ""
success = True

call(["getmail",])

logging.basicConfig(filename='log.txt',level=logging.DEBUG)
newmails = glob.glob("/home/pi/scanMail/new/*.nodepi")
scandir = "/home/pi/nodescan/"
formats = ["jpg", "JPG", "PNG", "png"]

for mail in newmails:
# get sender
try:
with open(mail, 'r') as f:
first_line = f.readline()
sender = re.search("<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>", first_line).group(1)
logText += "Successful parse:\n"
logText += sender + "/n"
logging.debug("Successful parse")
logging.debug(sender)
except:
logText += "Error in parsing %s\n" % mail
logging.debug("Error in parsing %s\n" % l)
success = False
break

# get image
try:
cmd = "munpack -C /home/pi/nodescan " + mail
cmd = "munpack -fq -C /home/pi/nodescan " + mail
os.system(cmd)
logText += "Succesfull outputting image munpack \n"
logging.debug("Succesfull outputting image munpack")
except:
logText += "Error in outputting image munpack \n"
logging.debug("Error in outputting image munpack")
success = False
break


images = glob.glob("/home/pi/nodescan/*.jpg") + glob.glob("/home/pi/nodescan/*.JPG")
images = flatten(map(lambda x: glob.glob("/home/pi/nodescan/*."+x), formats))
if len(images) > 0:
try:
check_call(["python", scandir + "scan.py", "--image", images[0]])
logText += "Successfully used scan.py \n"
logging.debug("Successfully used scan.py")
except:
logText += "Problem with scan.py \n"
logging.debug("Problem with scan.py")
success = False
break

try:
# send back the ok message with the file attached
cmd = "mutt -s 'Voila ton scan !' -i /home/pi/nodescan/ok_msg.txt " + sender + " -a /home/pi/*.pdf < /dev/null"
os.system(cmd)
logText += "Successfully send the scaned image \n"
logging.debug("Successfully send the scaned image")
except:
logText += "Problem sending mail \n"
logging.debug("Problem sending mail")
success = False
break
else:
# there is no image to process
# send back the "no image attached" error message
cmd = "mutt -s 'Euh, elle est ou ton image ?' -i /home/pi/nodescan/error_empty_msg.txt " + sender + " < /dev/null"
os.system(cmd)
logText += "no image in mail \n"
logging.debug("no image in mail")
success = False
break

try:
# clean
os.remove(mail)
os.system("rm -f /home/pi/nodescan/*.JPG.* /home/pi/nodescan/*.jpg.* /home/pi/nodescan/*.desc.* /home/pi/*.pdf.*")
os.system("rm -f /home/pi/scanMail/sent/cur/*")
logText += "Successfully cleaned files \n"
except:
logText += "Problem in cleaning files \n"
success = False
break
# try:
# clean
os.remove(mail)
cmd = "rm -f " + map(lambda x: "/home/pi/nodescan/*."+x, formats +["pdf, desc"]).join(" ")
os.system(cmd)
os.system("rm -f /home/pi/scanMail/sent/cur/*")
logging.debug("Successfully cleaned files")
# except:
# logging.debug("Problem in cleaning files")
# success = False
# break


if success != True:
# the image was not correctly processed
# send back the "file not processed" error message
tmp = open("/home/pi/nodescan/tmp.txt", "w")
tmp.write(logText)
tmp.close()
cmd = "mutt -s 'Probleme de scan...' -i /home/pi/nodescan/tmp.txt " + "bxnode.scan@gmail.com" + " < /dev/null"
cmd = "mutt -s 'Probleme de scan...' -i /home/pi/nodescan/log.txt " + "bxnode.scan@gmail.com" + " < /dev/null"
os.system(cmd)
cmd = "mutt -s 'Erreur de scan' -i /home/pi/nodescan/error_process_msg.txt " + sender + " < /dev/null"
os.system(cmd)
Expand All @@ -96,9 +96,6 @@
os.system("rm -f /home/pi/scanMail/sent/cur/*")
except:
print "error in cleaning"
logText += "Problem in cleaning files with errors \n"
logging.debug("Problem in cleaning files with errors")


logs = open("log.txt", "a")
logs.write(logText)
logs.close()
3 changes: 1 addition & 2 deletions scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import matplotlib.cm as cm
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image to be scanned")
ap.add_argument("-i", "--image", required = True, help = "Path to the image to be scanned")
args = vars(ap.parse_args())

# load the image and compute the ratio of the old height
Expand Down
38 changes: 38 additions & 0 deletions test_contours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pyimagesearch.transform import four_point_transform
from pyimagesearch import imutils
from skimage.filter import threshold_adaptive
import numpy as np
import argparse
import cv2
import pylab as plt
import matplotlib.cm as cm

image = cv2.imread("/Users/vallette/Desktop/photo.JPG")
# image = cv2.imread("/Users/vallette/Desktop/IMG_2827.JPG")

b,g,r = cv2.split(image) # get b,g,r
image = cv2.merge([r,g,b]) # switch it to rgb

ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)

edged = cv2.Canny(image, 75, 200)

(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)



fig = plt.figure(frameon=False)


plt.imshow(image)
# plt.savefig("out.pdf", format='pdf')
for contour in cnts:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
print len(approx)
cntr = np.array(map(lambda x: x[0], approx)).T
plt.plot(cntr[0], cntr[1])
plt.show()

0 comments on commit 6f74c4c

Please sign in to comment.