import sys
import os
import re
from time import gmtime, strftime
import pygtk
import gtk
import clips
import gst
import tempfile
GST_PIPE = ['v4l2src', 'ffmpegcolorspace', 'pngenc']
from sugar.activity import activity
from sugar.graphics import style
from gettext import gettext as _
class HelloDoctor(activity.Activity):
def __init__(self, handle):
"Entry point"
activity.Activity.__init__(self, handle)
self._name = handle
toolbox = activity.ActivityToolbox(self)
activity_toolbar = toolbox.get_activity_toolbar()
activity_toolbar.keep.props.visible = False
activity_toolbar.share.props.visible = False
self.set_toolbox(toolbox)
toolbox.show()
self.set_title(_("HelloDoctor"))
# self.window.set_border_width(5)
# self.window.set_size_request(640, 480)
self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
self.settings = gtk.Settings()
self.settings.set_string_property('gtk-font-name', 'courier bold 10', '')
self.settings.set_long_property("gtk-button-images", False, "")
self._main_view = gtk.EventBox()
self._main_view.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("khaki"))
self.set_canvas(self._main_view)
self.homescreen()
def homescreen(self, widget=None, oldcontainer=None):
homebox=gtk.VBox()
table= gtk.Table(20, 12, True)
colorbutton = gtk.ColorButton(gtk.gdk.color_parse("khaki"))
colorbutton.connect('color-set', self.set_background_color)
color = colorbutton.get_color()
title_label=gtk.Label()
labeltext_title=(_("Hello \nDoctor!"))
title_label.set_markup("%s" % (labeltext_title))
title_button=gtk.Button()
title_button.connect("clicked", self.symptomChose, homebox, '')
title_button_img=gtk.Image()
title_button_img.set_from_file("images/interface/start_button.svg")
title_button.set_image(title_button_img)
title_image=gtk.Image()
title_image.set_from_file("images/interface/doctor.svg")
table.attach(colorbutton, 1, 3, 15, 18)
table.attach(title_label, 0, 4, 4, 9)
table.attach(title_image, 4, 12, 0, 15)
table.attach(title_button, 7, 10, 14, 19)
homebox.pack_start(table)
if oldcontainer != None:
self._main_view.remove(oldcontainer)
oldcontainer.destroy()
self._main_view.add(homebox)
self._main_view.show()
self.symptomMain()
self.show_all()
def set_background_color(self, colorbutton):
color = colorbutton.get_color()
self._main_view.modify_bg(gtk.STATE_NORMAL, color)
def symptomMain(self):
global curr_counter_number, counter_max, list
list = []
rulesfile = open("rules.clp", "r")
for line in rulesfile:
matched_expr = re.search(re.escape("MAIN::symptom")+"(.*?)"+re.escape(" "),line)
if matched_expr==None:
continue
toprin = matched_expr.group(1)
list.append([toprin, ''])
finiline = line.find('))')
if finiline != -1:
break
curr_counter_number=0
counter_max=len(list) -1
def symptomChose(self, widget, oldcontainer, value):
global curr_counter_number, counter_max, list
if (value == "plus"):
curr_counter_number=curr_counter_number+1
if (value == "minus"):
curr_counter_number=curr_counter_number-1
symptom = list[curr_counter_number][0]
fraction = 0.05 + (curr_counter_number/(counter_max+1.0))
symptombox=gtk.VBox()
symptom_table= gtk.Table(20, 14, True)
progress=gtk.ProgressBar()
progress.set_fraction(fraction)
progress.set_text("%s/%s %s" % ((curr_counter_number+1),(counter_max+1),symptom))
symptom_image=gtk.Image()
symptom_image.set_from_file("images/symptoms/%s.svg" % (symptom))
button_yes=gtk.ToggleButton()
button_no=gtk.ToggleButton()
button_yes.connect("toggled", self.toggle_callback, "yes", button_no)
button_no.connect("toggled", self.toggle_callback, "no", button_yes)
if (list[curr_counter_number][1] == 'yes'):
button_yes.set_active(True)
elif (list[curr_counter_number][1] == 'no'):
button_no.set_active(True)
but_yes_img=gtk.Image()
but_yes_img.set_from_file("images/interface/oktick.svg")
button_yes.set_image(but_yes_img)
but_no_img=gtk.Image()
but_no_img.set_from_file("images/interface/oktick.svg")
button_no.set_image(but_no_img)
symptom_table.attach(button_yes, 3, 5, 17, 20)
symptom_table.attach(button_no, 9, 11, 17, 20)
button_next=gtk.Button()
but_next_img=gtk.Image()
but_next_img.set_from_file("images/interface/next_but.svg")
button_next.set_image(but_next_img)
nextcon=button_next.connect("clicked", self.symptomChose, symptombox, "plus")
if (curr_counter_number != 0):
button_prev=gtk.Button()
but_prev_img=gtk.Image()
but_prev_img.set_from_file("images/interface/prev_but.svg")
button_prev.connect("clicked", self.symptomChose, symptombox, "minus")
button_prev.set_image(but_prev_img)
symptom_table.attach(button_prev, 0, 2, 17, 20)
if (curr_counter_number == counter_max):
button_next.disconnect(nextcon)
button_next.connect("clicked", self.finishdialog, symptombox)
symptom_table.attach(progress, 1, 13, 0, 1)
symptom_table.attach(symptom_image, 1, 13, 1, 17)
symptom_table.attach(button_next, 12, 14, 17, 20)
symptombox.add(symptom_table)
print "values in symptomChose: curr: %s max: %s" % (curr_counter_number, counter_max)
self._main_view.remove(oldcontainer)
oldcontainer.destroy()
self._main_view.add(symptombox)
self.show_all()
def toggle_callback(self, widget, data, secondone):
global list
if (widget.get_active()):
list[curr_counter_number][1]=data
secondone.set_active(False)
def finishdialog(self, widget, oldcontainer):
dialog = gtk.Dialog("Finish?", None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
# dialog.set_modal(True)
# self._main_view.set_transient_for(dialog)
dialogimage= gtk.Image()
dialogimage.set_from_file("images/interface/to_solution.svg")
dialog.vbox.pack_start(dialogimage, True, True, 0)
button1 = gtk.Button()
dialog.action_area.pack_start(button1, True, True, 0)
button2 = gtk.Button()
dialog.action_area.pack_start(button2, True, True, 0)
dialogimage.show()
dialogresult = dialog.run()
if (dialogresult == gtk.RESPONSE_ACCEPT):
self._main_view.remove(oldcontainer)
oldcontainer.destroy()
self.solution()
dialog.destroy()
elif (dialogresult == gtk.RESPONSE_REJECT):
dialog.destroy()
def solution(self):
solutionbox= gtk.EventBox()
solution_hands= gtk.HBox()
solution_table= gtk.Table(20, 14, True)
solutionbox.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
patientphoto = gtk.Image()
photo = Camera()
photo.Snap()
photopath = photo.snap_path
patientpixbuf = gtk.gdk.pixbuf_new_from_file_at_size(photopath, 500, 475)
patientphoto.set_from_pixbuf(patientpixbuf)
# patientphoto.set_from_file("%s" % photopath)
# patientpixbuf = gtk.gdk.pixbuf_new_from_file_at_size(photopath, 300, 500)
# patientpixbuf.pixbuf
logfile = open('logfile.txt', 'a')
currtime = strftime("%H:%M:%S, %d %b %Y", gmtime())
towrite = str('---------%s---------\n' % currtime)
logfile.write(towrite)
logfile.close()
clips.Clear()
clips.Load("rules.clp")
clips.Reset()
beginingstate= clips.FactList()
total_diseases= 0
for bs in beginingstate:
if bs.Relation == 'disease':
total_diseases += 1
print total_diseases
for i in list:
sym = i[0]
occ = i[1]
if occ == '':
occ = 'no'
clips.Assert("(symptom %s %s)" % (sym,occ))
clips.Run()
result= clips.FactList()
numof_res = 0
for f in result:
if f.Relation == 'disease':
numof_res += 1
dis = f.CleanPPForm()
number = re.search(re.escape("(disease ")+"(.*?)"+re.escape(" "), dis)
justonedis = number.group(1)
total = map(int, justonedis)
print total
solution_image = gtk.Image()
if numof_res == total_diseases:
# solution_image.set_from_file("%s" % photopath)
solution_image.set_from_file("images/interface/healthy.svg")
else:
solution_image.set_from_file("images/interface/to_hospital.svg")
num_label2 = gtk.Label()
if numof_res == 0:
sollab_text = '000'
elif numof_res == 1:
sollab_text = justonedis
else:
sollab_text = 'UNK'
solution_table.attach(num_label2, 4, 7, 14, 16)
num_label2.set_markup("%s" % (sollab_text));
solution_table.attach(patientphoto, 0, 7, 1, 10)
solution_table.attach(solution_image, 2, 14, 1, 19)
reload_button = gtk.Button()
reload_button_img = gtk.Image()
# reload_button_img.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_BUTTON)
reload_button_img.set_from_file("images/interface/reload.svg")
reload_button.set_image(reload_button_img)
reload_button.connect("clicked", self.homescreen, solution_table)
solution_table.attach(reload_button, 12, 14, 17, 20)
self._main_view.add(solution_table)
self.show_all()
class Camera(object):
"""A class repre camera"""
def __init__(self):
snap_file, self.snap_path = tempfile.mkstemp()
pipe = GST_PIPE + ['filesink location=%s' % self.snap_path]
self.pipe = gst.parse_launch('!'.join(pipe))
self.bus = self.pipe.get_bus()
def Snap(self):
"""Take a snapshop"""
self.pipe.set_state(gst.STATE_PLAYING)
self.bus.poll(gst.MESSAGE_EOS, -1)
self.pipe.set_state(gst.STATE_NULL)
# if __name__ == "__main__":
#
# HelloDoctor().main()
# gtk.main()