def detect_barcode(f): jpgname = 'temp/temp.jpg' jpgbar = 'temp/tempbar.jpg' f.save(jpgname) # images_from_path = convert_from_path(pdfname, dpi=300, last_page=1, first_page =1) # for page in images_from_path: # page.save(jpgname) image = cv2.imread(jpgname) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, ksize=-1) gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1) gradient = cv2.subtract(gradX, gradY) gradient = cv2.convertScaleAbs(gradient) blurred = cv2.blur(gradient, (9, 9)) (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (80, 3)) closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) closed = cv2.erode(closed, None, iterations=25) closed = cv2.dilate(closed, None, iterations=25) cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = sorted(cnts, key=cv2.contourArea, reverse=True)[0] rect = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect) box = np.int0(box) x1, x2, x3, x4, y1, y2, y3, y4 = box[0][0], box[1][0], box[2][0], box[3][ 0], box[0][1], box[1][1], box[2][1], box[3][1] top_left_x = min([x1, x2, x3, x4]) top_left_y = min([y1, y2, y3, y4]) bot_right_x = max([x1, x2, x3, x4]) bot_right_y = max([y1, y2, y3, y4]) img = image[top_left_y + 6:bot_right_y + 6, top_left_x - 10:bot_right_x + 10] image_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imwrite(jpgbar, image_gray) code = decode(cv2.imread(jpgbar))[0][0] c1 = code.decode('ascii') nom_form = c1[0:4] reg = int(c1[4:7]) tablas = 'f' + nom_form exec("qry = db_session.query(" + tablas + ").filter(" + tablas + ".id==" + str(reg) + ").first()") qry1 = locals()['qry'] qry1.fscan = str(date.today()) flag_modified(qry1, 'fscan') db_session.merge(qry1) db_session.flush() db_session.commit() name = nom_form[1:4] + str(reg).zfill(4) + ".jpg" filename = "temp/" + name # os.rename(jpgname, filename) return jpgname, name
def plano_edit(): if request.method == 'GET': id = request.args.get('id', None) qry = db_session.query(plano).filter(plano.id == id).first() return render_template('plano_edit.html', val=qry) if request.method == 'POST': form = plano_form(request.form) fo = plano_convert(plano(), form) flag_modified(fo, 'plano') db_session.merge(fo) db_session.flush() db_session.commit() return redirect('/plano_list')
def folderedit(id): qry = db_session.query(Folders).filter(Folders.id == id) folders = qry.first() if folders: form = FoldersForm(formdata=request.form, obj=folders) if request.method == 'POST' and form.validate(): folder = FolderssConvert(Folders(), form) flag_modified(folder, 'name') db_session.merge(folder) db_session.flush() db_session.commit() flash('updated successfully!') return redirect('/folderlist') return render_template('folderform.html', form=form) else: return 'Error loading #{id}'.format(id=id)
def sheetedit(id): qry = db_session.query(Sheets).filter(Sheets.id == id) sheets = qry.first() if sheets: form = SheetsForm(formdata=request.form, obj=sheets) if request.method == 'POST' and form.validate(): sheet = SheetsConvert(Sheets(), form) flag_modified(sheet, 'name') db_session.merge(sheet) db_session.flush() db_session.commit() flash('updated successfully!') return redirect('/asheetlist') return render_template('sheetform.html', form=form) else: return 'Error loading #{id}'.format(id=id)
def attachmentedit(id): qry = db_session.query(Attachments).filter(Attachments.id == id) attachments = qry.first() if attachments: form = AttachmentsForm(formdata=request.form, obj=attachments) if request.method == 'POST' and form.validate(): attachment = AttachmentsConvert(Attachments(), form) flag_modified(attachment, 'name') db_session.merge(attachment) db_session.flush() db_session.commit() flash('updated successfully!') return redirect('/attachmentlist') return render_template('attachmentform.html', form=form) else: return 'Error loading #{id}'.format(id=id)