|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +##################################################### |
| 5 | +# # |
| 6 | +# Labitrack v 0.1 - (Beerware) - 2010 panton, halfd # |
| 7 | +# # |
| 8 | +##################################################### |
| 9 | + |
| 10 | + |
| 11 | +import Tkinter as T |
| 12 | +import barcode.barcode as B |
| 13 | +import opencv |
| 14 | +#import opencv.adaptors |
| 15 | +import ImageTk |
| 16 | +from opencv import highgui |
| 17 | + |
| 18 | +import os, sqlite3, random, time |
| 19 | + |
| 20 | +# Application class |
| 21 | +os.chdir('/home/panton/Documents/labitrack/') |
| 22 | + |
| 23 | +class Labitrack: |
| 24 | + def __init__(self, master): |
| 25 | + self.conn = sqlite3.connect('objects.db') |
| 26 | + self.cur = self.conn.cursor() |
| 27 | + |
| 28 | + # Get the next id man |
| 29 | + self.cur.execute('select oid+1 from objects order by oid desc limit 1;') |
| 30 | + self.id_val = self.cur.fetchone()[0] |
| 31 | + |
| 32 | + self.camera = highgui.cvCreateCameraCapture(0) |
| 33 | + self.image = None |
| 34 | + |
| 35 | + # Create a label instance |
| 36 | + self.label = B.Label() |
| 37 | + |
| 38 | + # The master frame |
| 39 | + self.frame = T.Frame(master) |
| 40 | + |
| 41 | + # Frame for camera input |
| 42 | + camera_frame = T.Frame(self.frame, relief="sunken", borderwidth=1, pady=20) |
| 43 | + |
| 44 | + self.imagecanvas = T.Canvas(camera_frame, width=320, height=240) |
| 45 | + |
| 46 | + # take picture button |
| 47 | + picture_button = T.Button(camera_frame, text="Take picture", anchor=T.W, command=self.takepicture) |
| 48 | + |
| 49 | + # Frame for input |
| 50 | + input_frame = T.Frame(self.frame, relief="sunken", borderwidth=1, pady=20) |
| 51 | + |
| 52 | + # Object name label and input field |
| 53 | + object_name_label = T.Label( |
| 54 | + input_frame, |
| 55 | + text="Name of item", |
| 56 | + font=("Helvetica", 17), |
| 57 | + anchor=T.W, |
| 58 | + justify=T.LEFT, |
| 59 | + width=50, |
| 60 | + padx=20, |
| 61 | + pady=5 |
| 62 | + ) |
| 63 | + |
| 64 | + self.object_name = T.Entry( |
| 65 | + input_frame, |
| 66 | + width=64, |
| 67 | + font=("Helvetica", 14), |
| 68 | + border=0, |
| 69 | + highlightcolor="#34fe32" |
| 70 | + ) |
| 71 | + |
| 72 | + # Object tagline label and input field |
| 73 | + object_tagline_label = T.Label( |
| 74 | + input_frame, |
| 75 | + text="Tagline", |
| 76 | + font=("Helvetica", 17), |
| 77 | + anchor=T.W, |
| 78 | + justify=T.LEFT, |
| 79 | + width=50, |
| 80 | + padx=20, |
| 81 | + pady=5 |
| 82 | + ) |
| 83 | + |
| 84 | + self.object_tagline = T.Entry( |
| 85 | + input_frame, |
| 86 | + width=64, |
| 87 | + font=("Helvetica", 14), |
| 88 | + border=0, |
| 89 | + highlightcolor="#34fe32" |
| 90 | + ) |
| 91 | + |
| 92 | + # Frame for options |
| 93 | + option_frame = T.Frame(self.frame, relief="sunken", border=1, pady=20, padx=20) |
| 94 | + |
| 95 | + self.personal = T.IntVar() |
| 96 | + self.choice_personal = T.Checkbutton(option_frame, text="Personal", variable=self.personal) |
| 97 | + |
| 98 | + self.manual = T.IntVar() |
| 99 | + self.choice_manual = T.Checkbutton(option_frame, text="Manual", variable=self.manual) |
| 100 | + |
| 101 | + self.dnh = T.IntVar() |
| 102 | + self.choice_dnh = T.Checkbutton(option_frame, text="Do not hack", variable=self.dnh) |
| 103 | + |
| 104 | + # Frame for buttons |
| 105 | + button_frame = T.Frame(self.frame, relief="sunken", border=1, pady=20, padx=20) |
| 106 | + |
| 107 | + # Preview button |
| 108 | + preview_button = T.Button(button_frame, text="Preview", anchor=T.W, command=self.create_preview) |
| 109 | + |
| 110 | + # Print button |
| 111 | + print_button = T.Button(button_frame, text="Print", anchor=T.E, command=self.create_print) |
| 112 | + |
| 113 | + # Pack camera input |
| 114 | + camera_frame.pack(side="left") |
| 115 | + self.imagecanvas.pack(side="top") |
| 116 | + picture_button.pack(side="top") |
| 117 | + |
| 118 | + # Pack input frame and widgets |
| 119 | + input_frame.pack(side="top",expand="true") |
| 120 | + |
| 121 | + object_name_label.pack(side="top") |
| 122 | + self.object_name.pack(side="top", expand="true") |
| 123 | + |
| 124 | + object_tagline_label.pack(side="top") |
| 125 | + self.object_tagline.pack(side="top") |
| 126 | + |
| 127 | + # Pack choice frame and widgets |
| 128 | + option_frame.pack(side="left",expand="true") |
| 129 | + |
| 130 | + self.choice_personal.pack(side="left") |
| 131 | + self.choice_manual.pack(side="left") |
| 132 | + self.choice_dnh.pack(side="left") |
| 133 | + |
| 134 | + # Pack button frame and widgets |
| 135 | + button_frame.pack(side="left") |
| 136 | + |
| 137 | + preview_button.pack(side="left") |
| 138 | + print_button.pack(side="left") |
| 139 | + |
| 140 | + # Set window-title and pack the master frame |
| 141 | + self.frame.master.title('Labitrack') |
| 142 | + self.frame.pack() |
| 143 | + #self.display_picture() |
| 144 | + |
| 145 | + def create_preview(self): |
| 146 | + self.name_val = self.object_name.get() |
| 147 | + self.tagline_val = self.object_tagline.get() |
| 148 | + self.personal_val = self.personal.get() |
| 149 | + self.manual_val = self.manual.get() |
| 150 | + self.dnh_val = self.dnh.get() |
| 151 | + |
| 152 | + self.cur.execute('select oid+1 from objects order by oid desc limit 1;') |
| 153 | + self.id_val = self.cur.fetchone()[0] |
| 154 | + |
| 155 | + self.label.create( |
| 156 | + self.name_val, |
| 157 | + self.tagline_val, |
| 158 | + self.id_val, |
| 159 | + labelpersonal=self.personal_val, |
| 160 | + labelmanual=self.manual_val, |
| 161 | + labeldnh=self.dnh_val |
| 162 | + ) |
| 163 | + |
| 164 | + def create_print(self): |
| 165 | + self.cur.execute('insert into objects (name, tagline) values (?, ?);', (self.name_val, self.tagline_val)) |
| 166 | + self.label.printlabel() |
| 167 | + self.conn.commit() |
| 168 | + |
| 169 | + def display_picture(self): |
| 170 | + imagecapture = highgui.cvQueryFrame(self.camera) |
| 171 | + self.pilimage = opencv.adaptors.Ipl2PIL(imagecapture) |
| 172 | + miniimage = self.pilimage.resize((320,240)) |
| 173 | + self.image = ImageTk.PhotoImage(miniimage, master=self.frame) |
| 174 | + self.imagecanvas.create_image(0,0,image=self.image,anchor="nw") |
| 175 | + self.imagecanvas.update() |
| 176 | + time.sleep(0.1) |
| 177 | + self.display_picture() |
| 178 | + |
| 179 | + def takepicture(self): |
| 180 | + self.pilimage.save('images/%d.png' % (self.id_val, )) |
| 181 | + |
| 182 | +# Create application and run mainloop |
| 183 | +top = T.Tk() |
| 184 | +labitrack = Labitrack(top) |
| 185 | +top.mainloop() |
0 commit comments