def detect_and_draw(self, img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints draw_color = QtGui.QColor(127, 255, 0) pen = QtGui.QPen(draw_color) pen.setWidth(2) aface = self.ui.scene.addRect( int(x * image_scale), int(y * image_scale), int(w * image_scale), int(h * image_scale), pen ) aface.setZValue(1) d("added") self.face_list.append(aface)
def stretch(self): curr_pixmap = self.temp_image height = curr_pixmap.height() zoom_interval = int(height * 0.2) self.zoom_time = self.zoom_time + 1 d("before:"+str(height)) l("after: "+str(height*1.2)) curr_pixmap = curr_pixmap.scaledToHeight(height+self.zoom_time*zoom_interval) self.current_image_item.setPixmap(curr_pixmap)
def on_item_double_clicked(self, item): l("open item: " + item.text()) if self.item_is_floder(item): self.openfolder(self.path + item.text()) elif self.item_is_image(item): self.hide() d("path :"+self.path + item.text()) sinview = SingleView(self.path + item.text()) sinview.exec_() self.show()
def keyPressEvent(self, key_event): d(key_event.key()) #try: operator = { QtCore.Qt.Key_W:self.turn_left, QtCore.Qt.Key_S:self.turn_right, QtCore.Qt.Key_A:self.click_previous, QtCore.Qt.Key_D:self.click_next, QtCore.Qt.Key_Q:self.stretch, QtCore.Qt.Key_E:self.reduce} operator.get(key_event.key())()
def show_image(self): d("index of image is "+ str(g_image_index)) d(g_image_list[g_image_index].abspath) self.photo = QtGui.QPixmap( g_image_list[g_image_index].abspath ) self.temp_image = QtGui.QPixmap(self.photo) self.zoom_time = 0 if self.photo : l('load image success') else: l('load image faild: check file '+image_path+' exist.') exit(1) if self.current_image_item: image_size = self.photo.size() #step 1: remove before image and face rectangle self.ui.scene.removeItem(self.current_image_item) self.reomve_face_rects() self.ui.scene.setSceneRect(0, 0, image_size.width(), image_size.height()) self.current_image_item = self.ui.scene.addPixmap(self.photo) else: self.current_image_item = self.ui.scene.addPixmap(self.photo)
def init_image_list(self, image_path): global g_image_index image_path = str(image_path) self.floder_name = os.path.dirname(os.path.abspath(image_path)) target_name = os.path.basename( image_path ) l("opening floder:"+ self.floder_name) l("opening target:"+ target_name) #clear the link list of image del g_image_list[:] g_image_index = 0 count = 0 for filename in os.listdir(self.floder_name): #find all image if isimage( filename ): l(filename) g_image_list.append(Picture( self.floder_name , filename)) if filename == target_name: d("found!") g_image_index = count count = count +1 l("this floder has "+str(len(g_image_list))+" photos") d("target index is "+ str(g_image_index))
def click_previous(self): global g_image_index d("print next pushButton") d("total:"+str(len(g_image_list))) d("current:"+str(g_image_index + 1)) if g_image_index > 0: g_image_index = g_image_index - 1 self.show_image()
def play(self): time.sleep(1) l("enter ppt mode") self.setWindowFlags(QtCore.Qt.Window) self.showFullScreen() tmp_image_index = g_image_index while tmp_image_index+1 <= len(g_image_list): image = QtGui.QPixmap( g_image_list[tmp_image_index].abspath ) label_width = self.ui.image_label.width() label_height = self.ui.image_label.height() if image.width() > label_width : image = image.scaledToWidth(label_width) if image.height() > label_height: image = image.scaledToHeight(label_height) elif image.height() > label_height: image = image.scaledToHeight(label_height) if image.width() > label_width: image = image.scaledToWidth(label_width) d("showing "+str(tmp_image_index)+":"+g_image_list[tmp_image_index].abspath) self.ui.image_label.setPixmap(image) time.sleep(3) tmp_image_index = tmp_image_index + 1 thread.exit_thread()
def evolve(self): # global inactivation_probability # global transposition_probability # global random_mutation_rate # global transposon_creation_probability self.transpositions = 0 # Autonomous transposon activity if self.aut_transposons > 1000000 or self.nonaut_transposons > 1000000: # this plant definitely wouldn't survive anyway, but causes technical problems and crashes if we let her pass this point... self.die() return # tranpozony ktore ulegly deautonomizacji # tj okreslamy liczbe transpozonow, ktore staly sie nieautonomiczne no_aut_decayed = distributions.rbinom(self.aut_transposons, p.deauton_probability) ##KG: I assume the following meaning of variables ##no_aut_decayed - no. autonomous TEs that became non-autonomous ## thus are moved to list on non-aut TEs ##no_aut_fixed - no. autonomous TEs that lost their functionality ## thus are deleted permanently ##KG: changes is the lists of TEs ##I choose no_aut_decayed TEs randomly from aut_list and move them new_nonaut_list = [] if p.sexual_mode: # wybieramy ktore staja sie nieautonomiczne which_aut_decayed = random.sample(range(self.aut_transposons), no_aut_decayed) # jezeli jest cokolwiek do zrobienia if len(which_aut_decayed) > 0 : #new_nonaut_list = [] tmp_aut_list = [] idx_decayed, idx_aut = 0, 0 # sortujemy indeksy ktore nalezy przeniesc which_aut_decayed.sort() # sprawdzamy kolejno wszystkie transpozony i przenosimy te ktore sa pod indeksami z which_aut_decayde for x in range(self.aut_transposons) : # do przeniesienia if idx_decayed < len(which_aut_decayed) and idx_aut == which_aut_decayed[idx_decayed] : new_nonaut_list.append(self.aut_transposons_list[idx_aut]) idx_decayed += 1 else : tmp_aut_list.append(self.aut_transposons_list[idx_aut]) idx_aut += 1 # zapisujemy nowy stan listy self.aut_transposons_list = tmp_aut_list # obecnie mamy tmp_aut_te TE autonomicznych na liscie tmp_aut_te = self.aut_transposons - no_aut_decayed # wybieramy ile transpozonow ulega calkowitej dezaktywacji, tzn zanikaja z systemu a ich wklad fenotypowy # na stale modyfikuje fenotyp hosta no_aut_fixed = distributions.rbinom(tmp_aut_te, p.inactivation_probability) ##KG ##I choose no_aut_fixed TEs randomly to remove them from list ##which_aut_fixed = random.sample(range(tmp_aut_te), no_aut_fixed).sort() if p.sexual_mode : try: if tmp_aut_te != len(self.aut_transposons_list): import sys debug.g("length of list does not equal to variable value") raise AssertionError("aut_list len != no_aut") sys.exit(1) else: # if there is anything to do if no_aut_fixed > 0 : which_aut_fixed = random.sample(range(tmp_aut_te), no_aut_fixed) idx_aut, fixed_idx = 0, 0 tmp_aut_list2 = [] which_aut_fixed.sort() # przegladamy liste wszystkich transpozonow while fixed_idx < len(which_aut_fixed) and idx_aut < tmp_aut_te : # jezeli trafiamy na numer ktory ulega dezaktywacji if idx_aut == which_aut_fixed[fixed_idx]: # zapamietujemy transpozon i zapisujemy jego zmiane na stale te_to_fix = self.aut_transposons_list[idx_aut] if p.multidim_changes: pass#TODO else: #add the change from TE to the base phenotype self.phenotype[te_to_fix.trait_no] += te_to_fix.mutation_rate fixed_idx += 1 else : tmp_aut_list2.append(self.aut_transposons_list[idx_aut]) idx_aut += 1 self.aut_transposons_list = tmp_aut_list2 #self.aut_transposons_list = random.sample(self.aut_transposons_list, self.aut_transposons - no_aut_decayed - no_aut_fixed) #self.aut_transposons_list.sort() except ValueError: import sys debug.g('SHIT F**K, HOW ?!') debug.g(self.aut_transposons_list) numbb = self.aut_transposons - no_aut_decayed - no_aut_fixed debug.d("choose = %d" % numbb) sys.exit(0) ###### len(aut) = self.aut.te - decayed - fixed no_aut_deleted = no_aut_fixed + no_aut_decayed #print "po usunieciach aut" #print "aut list dlug. > " + str(len(self.aut_transposons_list)) #print "aut ile > " + str(self.aut_transposons) no_new = None if p.autonomous_transp_dynamics == 'linear': no_new = distributions.rpois(p.transposon_creation_rate + (self.aut_transposons - no_aut_deleted) * p.transposition_rate) elif p.autonomous_transp_dynamics == 'square': no_new = distributions.rpois(p.transposon_creation_rate + (self.aut_transposons - no_aut_deleted) * p.transposition_rate * (self.aut_transposons - no_aut_deleted)) elif p.autonomous_transp_dynamics == 'old': no_new = distributions.rpois(p.transposon_creation_rate + (self.aut_transposons - no_aut_deleted) * p.transposition_rate * self.transposase_activity) else: raise Exception("autonomous_transp_dynamics parameter must be one of: 'linear', 'square', 'old', and not: " + str(p.autonomous_transp_dynamics)) self.transpositions += no_new non_duplicative = 0 if p.duplicative_transposition_probability != 1.0: non_duplicative = distributions.rbinom(no_new, 1.0 - p.duplicative_transposition_probability) no_aut_deleted += non_duplicative ##KG: ##we append no_new - non_duplicative TEs that has appeared ### THIS IS PROBABLY THE SHITTEST CODE EVER PRODUCED BY HUMAN... I will improve it one day if p.sexual_mode: which_to_copy = sample_wr(range(self.aut_transposons - no_aut_fixed - no_aut_decayed), no_new - non_duplicative) # powstaly nowe TE narodzone z niczego if no_new - non_duplicative > 0 and (self.aut_transposons - no_aut_fixed - no_aut_decayed) == 0 : for i in range(no_new - non_duplicative): te = Transposone(True) #self.phenotype.TE_driven_mutation(te.mutation_rate) self.aut_transposons_list.append(te) # powstaly TE zarowno przez kopiowanie jak i nowonarodzone elif no_new - non_duplicative > 0 and (self.aut_transposons - no_aut_fixed - no_aut_decayed) > 0 : tmp_no_new = distributions.rpois(p.transposon_creation_rate) # ilosc nowo powstalych if tmp_no_new > 0 : tmp_no_new = min(tmp_no_new, no_new - non_duplicative) for i in range(tmp_no_new): te = Transposone(True) #self.phenotype.TE_driven_mutation(te.mutation_rate) self.aut_transposons_list.append(te) for i in range(no_new - non_duplicative - tmp_no_new): parent_id = self.aut_transposons_list[which_to_copy[i]].id #parent_mut = self.aut_transposons_list[i].mutation_rate #self.phenotype.TE_driven_mutation(parent_mut) te = Transposone(True, parent_id) self.aut_transposons_list.append(te) # kopiowane else: for i in which_to_copy: parent_id = self.aut_transposons_list[i].id #parent_mut = self.aut_transposons_list[i].mutation_rate #self.phenotype.TE_driven_mutation(parent_mut) te = Transposone(True, parent_id) self.aut_transposons_list.append(te) #self.transposase_activity = p.get_transposase_change(self.transposase_activity, no_new, no_deleted, self.aut_transposons) self.transposase_activity = self.aut_transposons # OSTATECZNA liczba transpozonow autonomicznych self.aut_transposons += no_new - no_aut_deleted if p.sexual_mode: if self.aut_transposons != len(self.aut_transposons_list) : kur = len(self.aut_transposons_list) debug.g("number = %d" % self.aut_transposons) debug.g("while list size = %d" % kur) raise AssertionError("aut_list len != no_aut") # # OBLUSGA NIEAUTONOMICZNYCH # # liczba nowych transpozonow nieautonomicznych, ktore powstaly z autonomicznych new_nonautonomous = no_aut_decayed # Non-autonomous transposon activity #t = self.nonaut_transposons no_deleted = distributions.rbinom(self.nonaut_transposons, p.inactivation_probability) no_deleted_tmp = no_deleted ##KG ##I choose no_deleted nonaut TEs randomly to remove them from list if p.sexual_mode: #o ile cokolwiek jest do zrobienia if no_deleted > 0 : which_nonaut_deleted = random.sample(range(self.nonaut_transposons), no_deleted) idx_nonaut, deleted_idx = 0, 0 tmp_nonaut_list2 = [] which_nonaut_deleted.sort() # looking through all transposons while deleted_idx < len(which_nonaut_deleted) and idx_nonaut < self.nonaut_transposons : # transpozon do usuniecia. Zmieniamy baze fenotypu i przesuwamy sie do nastepnej pozycji if idx_nonaut == which_nonaut_deleted[deleted_idx]: te_to_delete = self.aut_transposons_list[idx_nonaut] if p.multidim_changes: pass #TODO else: #add the change from TE to the base phenotype self.phenotype[te_to_delete.trait_no] += te_to_fix.mutation_rate deleted_idx += 1 # nieciekawy przypadek, przenosimy te na nowa liste else: # transpozon zostaje w miejscu tmp_nonaut_list2.append(self.nonaut_transposons_list[idx_nonaut]) # przesuniecie po kazdym przypadku na liscie nonaut idx_nonaut += 1 #zapisujemy nowowybrane nonaut self.nonaut_transposons_list = tmp_nonaut_list2 #self.nonaut_transposons_list = random.sample(self.nonaut_transposons_list, self.nonaut_transposons - no_deleted) # liczba nowych nieautonomicznych zalezna od ilosci autonmicznych no_new = distributions.rpois((self.nonaut_transposons - no_deleted) * p.transposition_rate * self.transposase_activity) self.transpositions += no_new if p.duplicative_transposition_probability != 1.0: non_duplicative = distributions.rbinom(no_new, 1.0 - p.duplicative_transposition_probability) no_deleted += non_duplicative ##KG: ##we append no_new - non_duplicative nonaut TEs that has appeared if p.sexual_mode: # watpliwe miejsce - na poczatku przeciez i tak nie mamy nieautonomicznych chyba which_to_copy_2 = sample_wr(range(self.nonaut_transposons - no_deleted_tmp), no_new - non_duplicative) if self.nonaut_transposons == 0 : for i in which_to_copy_2 : te = Transposone(False) self.nonaut_tranposons_list.append(te) else : for i in which_to_copy_2 : parent_id = self.nonaut_transposons_list[i].id #parent_mut = self.nonaut_transposons_list[i].mutation_rate #self.phenotype.TE_driven_mutation(parent_mut) te = Transposone(False, parent_id) #POST-transposition change #self.phenotype.TE_driven_mutation(te.mutation_rate) self.nonaut_transposons_list.append(te) ##append TEs that became nonaut in this round self.nonaut_transposons_list += new_nonaut_list ###### self.nonaut_transposons = self.nonaut_transposons + no_new - no_deleted + new_nonautonomous new_inactive = no_deleted # Inactive transposon accumulation and decay self.inactive_transposons += new_inactive self.inactive_transposons -= distributions.rbinom(self.inactive_transposons, p.deletion_probability) # Impact of transpositions on plant # self.transposase_activity = p.get_transposase_change(self.transposase_activity, no_new, no_deleted, self.aut_transposons) #Environment.default.transpositions_in_this_generation += transpositions ############################# ## PHENOTYPE CHANGES BLOCK ## ############################# survival_likelihood = p.nonlethal_transposition_likelihood**self.transpositions if(not distributions.happened(survival_likelihood)): self.die() else: #self.phenotype.mutate(self.transpositions, stdev = p.transposition_mutation_stdev) tmp_mut = distributions.rpois(p.random_mutation_rate) self.phenotype.mutate(tmp_mut, stdev = p.non_transposition_mutation_stdev) self.total_mutations = tmp_mut + self.transpositions self.random_mutations = tmp_mut
def click_play(self): playdlg = SlideView() playdlg.exec_() d("play end")
def reomve_face_rects(self): for face in self.face_list: d("remove face rect..") self.ui.scene.removeItem(face) self.face_list = []