def task_gen_top_tag_via_number(query, K, root): from photo_dao import PhotoDao from database import DBHelper db_helper = DBHelper() db_helper.init(root) photo_dao = PhotoDao(db_helper) photo_ids = photo_dao.getClassPhotoIds(query, ''.join([query])) photos = photo_dao.getPhotos(query, photo_ids) hist = {} for photo in photos: tags = photo.tags for tag in tags: if(tag in hist): hist[tag] = hist[tag] + 1 else: hist[tag] = 0 top_word_freq = sorted(hist.items(), key=lambda t: -t[1]) top_word_freq = top_word_freq[0:min(len(top_word_freq), K)] top_word = [] for line in top_word_freq: top_word.append(line[0].strip()) output_path = ''.join([root, '/data/tags/%s.txt'%query]) from file_io import FileIO file_io = FileIO() file_io.write_strings_to_file(top_word, output_path) return top_word
def register(): if flask_request.method == 'POST': username = flask_request.form['username_field'] password = flask_request.form['password_field'] netID = flask_request.form['network_id_field'] hubID = flask_request.form['hub_id_field'] acu_id = flask_request.form['acu_id_field'] access_key = flask_request.form['access_key_field'] #send username and password information to CoMPES db = DBHelper() dbSuccess = db.createUser( [username, password, hubID, netID, acu_id, access_key]) if (dbSuccess): #sign in to CoMPES #process items from CoMPES #register face global comms comms.registerUser(username, password, netID, hubID, acu_id, access_key) comms.cam_factory.rec.is_registering = True resp = make_response(render_template('profile.html', user=username)) return resp else: #display message on webpage and have the user try again. return "failed register" else: return render_template("register.html")
def _register(self, frame, gray, username): """Detects faces in frames and uses them to train a face recognition algorithm. Face data is associated with the given username. :param frame: an 8 bit, 3-channel UMat object :param gray: an 8-bit, 1-channel ndarray :param username: name of registering user """ if self.samples < self.sample_size: # Find all faces and store only the location of the largest (closest to the camera) faces = self._findFaces(frame) max_area = 0 x = 0 y = 0 w = 0 h = 0 for (startX, startY, endX, endY) in faces: c_w = endX - startX c_h = endY - startY if c_w * c_h > max_area: x = startX y = startY w = c_w h = c_h maxArea = w * h # Resize and add face image to list for training if faces: self.samples += 1 gray = cv2.UMat(gray, [y, y + h], [x, x + w]) gray = cv2.resize(gray, (100, 100)) self.sample_images.append(gray) else: # Finished collecting face data # Associate registering user id with training data db = DBHelper() user_id = db.getIDByUsername(username) id_array = [user_id] * self.sample_size for i in range(self.sample_size): self.sample_images[i] = cv2.UMat.get(self.sample_images[i]) # Update or create new face recognizer if Path('./training_data/recognizer.yml').is_file(): self.recognizer.update(self.sample_images, np.array(id_array)) else: self.recognizer.train(self.sample_images, np.array(id_array)) self.recognizer.write('./training_data/recognizer.yml') # registration complete self.reg_complete = True self.rec_trained = True # reset variables before detection begins self._reset() return frame
def get_photo_imagepath(root, query, image_ids): db = DBHelper() db.init(root) imagepaths =[] for image_id in image_ids: imagepath = db.getPhotoImgPath(query, image_id) imagepaths.append(imagepath) return imagepaths
def connect(): #process users credentials here. db = DBHelper(True) #close the connection in this function. username = flask_request.form['username_field'] password = flask_request.form['password_field'] authSuccess = db.authenticate([username, password]) if (authSuccess): #make the connection to compess here. hub_id = db.getHubIdByUsername(username) net_id = db.getNetIdByUsername(username) acu_id = db.getACUByUsername(username) access_key = db.getAccessKeyByUsername(username) comms.registerUser(username, password, net_id, hub_id, acu_id, access_key) #comms.web_factory.rec.is_registering = True resp = make_response(render_template('active.html', user=username)) return resp else: return render_template( 'index.html', message="Failed to authenticate. Please try again. ") db.disconnect()
def taskBuildAllFeatures(): from photo_dao import PhotoDao from database import DBHelper db_helper = DBHelper() query = 'beauty' db_helper.init('D:/Iconic') photoDao = PhotoDao(db_helper) photoIds = photoDao.getClassPhotoIds(query, ''.join([query])) featureDao = FeatureDao() features = featureDao.read_features(query, photoIds, 'tag3000') featureDao.save_features(query, photoIds, 'tag3000', features)
def check_deadline(update, context): db = DBHelper(CONFIG["db_name"]) db.clear_old_deadlines() dls = db.fetch_deadlines(update.effective_chat.id) header = None message_string = "<b>Deadlines</b>\n" for _, dl in dls: if header != dl.friendly_date(): header = dl.friendly_date() message_string += "\n<b>{}</b>\n".format(header) message_string += html.escape(str(dl)) message_string += "\n" delete_old_listings(update, context) m = update.message.reply_html(message_string) add_old_listing(update, context, m)
def new_deadline(update, context): """Insert new deadline into deadline list""" db = DBHelper(CONFIG["db_name"]) args = " ".join(context.args) try: dl = deadline.Deadline.parse(args) if dl.in_how_many_days() < -1: update.message.reply_text( "You're a bit too late to submit something on {}".format( dl.time.strftime("%d %b %Y, %H:%M:%S"))) else: # actually handle the deadline db.insert_deadline(update.effective_chat.id, dl) update.message.reply_text("Succesfully added {}".format(str(dl))) except ValueError as e: update.message.reply_text(str(e))
def connect(): #process users credentials here. db = DBHelper(True) #close the connection in this function. username = flask_request.form['username_field'] password = flask_request.form['password_field'] authSuccess = True #db.authenticate([username, password]) if (authSuccess): return redirect(url_for('active')) else: return render_template( 'index.html', message="Failed to authenticate. Please try again. ") db.disconnect()
def task_gen_photo_imagepath(root, query): print('Get photo ids.'); db_helper = DBHelper(); db_helper.init(root); photo_dao = PhotoDao(db_helper); tic(); photo_ids = photo_dao.getClassPhotoIds(query, ''.join([query])); toc(); print('Get photo path.'); imagepaths = get_photo_imagepath(root, query, photo_ids) output_path = ''.join([db_helper.datasetDir, '/', query, '_imagepath.txt']); file_io = FileIO(); file_io.write_strings_to_file(imagepaths, output_path);
def remove_deadline_by_index(update, context): db = DBHelper(CONFIG["db_name"]) delete_old_listings(update, context) text = re.sub(r"/(\d+)(@{})?$".format(re.escape(context.bot.username)), r"\g<1>", update.message.text) try: index = int(text) dl_id, dl = context.user_data["remove_list"][index] db.delete_deadline(dl_id) update.message.reply_text(str(dl) + " removed!") check_deadline(update, context) except KeyError as ke: update.message.reply_text(str(index) + " not in list!") except ValueError as ve: update.message.reply_text(text + " is not a valid integer!") context.user_data["remove_list"] = None return -1
def start_remove_deadline(update, context): db = DBHelper(CONFIG["db_name"]) db.clear_old_deadlines() dls = db.fetch_deadlines(update.effective_chat.id) header = None message_string = "<b>Deadlines</b>\n" context.user_data["remove_list"] = {} for i, (dl_id, dl) in enumerate(dls): if header != dl.friendly_date(): header = dl.friendly_date() message_string += "\n<b>{}</b>\n".format(header) message_string += "/{} {}".format(i + 1, html.escape(str(dl))) context.user_data["remove_list"][i + 1] = (dl_id, dl) message_string += "\n" m = update.message.reply_html(message_string) delete_old_listings(update, context) add_old_listing(update, context, m) return CHOOSING
def task_gen_photo_meta(root, query, do_save_meta): print('generating photo meta for %s'%(query)); filter = PhotoFilter(); dbHelper = DBHelper(); dbHelper.init(root); photos = filter.get_photo_with_tag_and_unique(query, dbHelper); if (do_save_meta): photo_dao = PhotoDao(dbHelper) for photo in photos: photo_dao.savePhotoMeta(query, photo); photos = filter.filter_photo_without_tags(photos); outputPath = ''.join([dbHelper.datasetDir, '/', query, '.txt']); print(outputPath); fout = open(outputPath, 'w'); for photo in photos: fout.write(photo.photoId) fout.write('\n') fout.close();
def register(): if flask_request.method == 'POST': username = flask_request.form['username_field'] password = flask_request.form['password_field'] db = DBHelper() dbSuccess = db.createUser([username, password]) if (dbSuccess): global comms comms.registerUser(username, password) #comms.cam_factory.rec.is_registering = True resp = make_response(render_template('profile.html', user=username)) return resp else: #display message on webpage and have the user try again. return "failed register" else: return render_template("register.html")
def main(): """Start the bot.""" # Create the Updater and pass it your bot's token. # Make sure to set use_context=True to use the new context based callbacks # Post version 12 this will no longer be necessary db = DBHelper(CONFIG["db_name"]) db.setup() updater = Updater(CONFIG["bot_token"], use_context=True) # Get the dispatcher to register handlers dp = updater.dispatcher # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("newdl", new_deadline)) dp.add_handler(CommandHandler("chkdl", check_deadline)) dp.add_handler( ConversationHandler( entry_points=[CommandHandler("remdl", start_remove_deadline)], states={ CHOOSING: [ MessageHandler(Filters.regex(r"^/\d+(@shimekiribot)?$"), remove_deadline_by_index) ] }, fallbacks=[])) # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def task_gen_index_by_tag(query, root, top_tag_num, top_tags, photo_ids, output_root): from photo_dao import PhotoDao from database import DBHelper db_helper = DBHelper() db_helper.init(root) photo_dao = PhotoDao(db_helper) photos = photo_dao.getPhotos(query, photo_ids) top_tags_index = {} for i, tag in enumerate(top_tags): top_tags_index[tag] = i tag_image_index = ['']*top_tag_num for photo in photos: tags = photo.tags for tag in tags: if(tag in top_tags): tag_index = top_tags_index[tag] tag_image_index[tag_index] = ''.join([tag_image_index[tag_index], ',', photo.photoId]) web_dao = WebPageResultDao() web_dao.init(output_root) for key in top_tags: tag_index = top_tags_index[key] line = tag_image_index[tag_index] line = line.strip() if (line != ""): tag_image_ids = [] image_ids = line.split(',') for image_id in image_ids: image_id = image_id.strip() if (image_id != ""): tag_image_ids.append(image_id) try: web_dao.save_photo_ids('tag_images/%s' % key, '1', tag_image_ids) except: print('error in generating %s' % key)
def onMessage(self, data, isBinary): """ Description: Decodes the image sent from the camera """ #STEP 1: Load in, convert, and decompress frame for use frame = ujson.loads(data.decode("utf8")) frame = np.asarray(frame, np.uint8) frame = cv2.imdecode(frame, cv2.IMREAD_COLOR) #post users client name here. #frame = message if (self.factory.bridge.user is not None): print(self.factory.bridge.user) frame, username, gesture = self.factory.rec.processFrame( frame, self.factory.bridge.user) if (gesture != '0'): #gesture is '0' by default db = DBHelper(True) gest_func = db.getGestureFunction(username, "gest_" + str(gesture)) acu = db.getACUByUsername(username) tag = acu + ",," + str(gest_func) if gest_func != None: self.factory.bridge.sendTag(tag) if (self.factory.rec.is_registering == False and self.factory.rec.reg_complete == True): self.factory.bridge.web_factory.connections["client1"].sendMessage( "registration".encode("UTF8")) frame = cv2.UMat(frame) frame = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 20])[1] frame = base64.b64encode(frame) #send to web factory self.factory.bridge.web_factory.post(self.factory.bridge.user, frame)
def associations(): if flask_request.method == 'POST': try: #register the associations here. gestureDict = {} gestureDict['gest_1'] = flask_request.form['gest_1'] gestureDict['gest_2'] = flask_request.form['gest_2'] gestureDict['gest_3'] = flask_request.form['gest_3'] gestureDict['gest_4'] = flask_request.form['gest_4'] gestureDict['gest_5'] = flask_request.form['gest_5'] print(str(gestureDict)) print(comms.user) db = DBHelper(True) #open in passive mode for key in gestureDict: db.addGesture(comms.user, key, gestureDict[key]) db.disconnect() return render_template('active.html') except: return "Error processing form. " else: SRO = comms.getSRO() states = ujson.loads(SRO)['lab-cam']['States'] states_str = ','.join(states) db = DBHelper(True) current_states = [] for x in range(1, 6): gest = db.getGestureFunction(comms.user, 'gest_' + str(x)) current_states.append( gest) if gest != None else current_states.append('') current_states = ','.join(current_states) resp = make_response( render_template('associations.html', default=states_str, data=current_states)) return resp
def getFeaturePath(self, query, photoId, featureId): from database import DBHelper db_helper = DBHelper() subPath = db_helper.getPhotoSubDir(photoId) featurePath = ''.join([self.featureRoot ,'/', featureId, '/',query , '/' , subPath , '/', '%s_%s.mat'%(featureId, photoId)]) return featurePath
import config from flask import Flask, request, Response, json from database import DBHelper, DBHelperError from datetime import date, datetime app = Flask(__name__) db_helper = DBHelper(**config.get_db_requisites()) logs_dir_path = config.get_logs_dir_path() @app.route('/imports', methods=['POST']) def import_data(): try: import_id = db_helper.import_citizens(request.json) except DBHelperError as e: return Response(response=str(e), status=400) else: return Response(response=json.dumps({"data": {"import_id": import_id}}), status=201, mimetype='application/json') @app.route('/imports/<int:import_id>/citizens/<int:citizen_id>', methods=['PATCH']) def change_citizen_data(import_id, citizen_id): try: citizen_data = db_helper.change_citizen(import_id, citizen_id, request.json) except DBHelperError as e: return Response(response=str(e), status=400) else: return Response(response=json.dumps({"data": citizen_data}), status=200,
photo_files = self.get_photo_meta_files(query, db_helper) photos = [] photoBuilder = PhotoBuilder(); for filepath in photo_files: items = photoBuilder.get_photo_from_file(filepath) photos.extend(items) return photos def get_photo_with_tag_and_unique(self, query, db_helper): metaFilePath = db_helper.getRawMetaFileDir(query); photos = self.get_photo_with_tag(metaFilePath); # photos = self.get_photo_metas(query, db_helper) photos = self.filter_photo_tags(photos); print(len(photos)); photos = self.make_unique_user(photos); print(len(photos)); return photos; if __name__ == "__main__": root = '../../../../test_dir' db_helper = DBHelper() db_helper.init(root) filter = PhotoFilter() photos = filter.get_photo_meta_files('art', db_helper) print (photos[0:10]) print (len(photos))
# submit_visit - submit data visit ke bot # code_csv - mengunduh list kode visit (file csv) # todo_list - input list pelanggan yang akan divisit # help_todo_list - lihat cara penggunaan todo list # help - lihat contoh penggunaan bot # cancel - membatalkan sesi input visit # start_adm1n - masuk ke dalam sesi admin # start - mulai chat bot # case conversation handler admin PASSWD_ADMIN, EDIT_RV_ADMIN, ADD_RV, UDPATE_NAME_RV, UPDATE_CODE_RV, REMOVE_RV, RENAME_RV, RECODE_RV, \ CATEGORY_RESULT_ADMIN, VISIT_RESULT_ADMIN, MENU_ADMIN, PIN_CHANGE, NEW_PIN, LAPORAN_ADMIN, \ EDIT_CR_ADMIN, VISIT_MENU_ADMIN, ADD_CR, RENAME_CR, RECODE_CR, REMOVE_CR, ADD_SV, RENAME_SV, RECODE_SV, \ REMOVE_SV, EDIT_SV_ADMIN, DATE_LAST, DATE_SELECTED, ADMIN_CHOOSE_OPSI, INPUT_USERID = range(1, 30) db = DBHelper() session = Session() logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) state_rv = -1 state_cv = -1 pin_admin = "" admin_msg_id = 0 admin_chat_id = 0 code_msg_id = 0 code_chat_id = 0 entry_msg_id = 0 entry_chat_id = 0 lpr_date_start = ""
def _detect(self, frame, gray): """Detects faces, compares them registered faces, and detects hands for gesture recognition if a match is found. :param frame: a BGR color image for display :param gray: a grayscale copy of the passed BGR frame :returns: (out_frame, username, gesture) the processed frame for display on webpage, the detected user, the detected gesture """ username = "" gesture = "0" num_fingers = 0 if self.gesture_tracker is None: # not currently tracking hands faces = self._findFaces(frame) for (startX, startY, endX, endY) in faces: # text is displayed at y coordinate y = startY - 10 if startY - 10 > 10 else startY + 10 # gray_face sometimes gets converted back to ndarray, throwing an error # I do not know why try: gray_face = cv2.UMat(gray, [startY, endY], [startX, endX]) except: gray_face = gray[startY:endY, startX:endX] # optional resize for slightly improved performance gray_face = cv2.resize(gray_face, (100, 100)) user_id, confidence = self.recognizer.predict(gray_face) gray = cv2.UMat.get(gray) # mask detected face region with solid black to avoid false positives in hand detection gray[startY:endY, startX:endX] = self.black_mask[startY:endY, startX:endX] # for LBPH recognizer, lower confidence scores indicate better results if confidence <= 80: # user is recognized db = DBHelper() username = db.getUsernameById(user_id) cv2.putText(frame, username, (startX, y), self.font, .6, (225, 105, 65), 2) else: # face belongs to unknown user cv2.putText(frame, "unknown", (startX, y), self.font, .6, (0, 0, 255), 2) # a user is recognized and hand detection begins if username is not "" and faces: hands = self.hand_classifier.detectMultiScale(gray, 1.3, 5) # detected hand region is resized to allow for tracking an open hand for (x, y, w, h) in hands: x_mid = (w // 2) y = int(y - h * 1.3) x = int(x - x_mid * 1.5) w = int(w + 3 * x_mid) h = int(h * 2 + h * 0.7) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # only attempt to recognize hand gesture if background model is finished calibrating if self.bg_model.calibrated: self.gesture_tracker = GestureTracker( frame, (x, y, w, h)) # if no faces are in the frame, assume the frame is background if not self.bg_model.calibrated and not faces: self.bg_model.runAverage(frame) else: # hand has been detected and is being tracked by gesture_tracker timed_out, (x, y, w, h) = self.gesture_tracker.update(frame) if timed_out: self.gesture_tracker = None try: gray = cv2.UMat.get(gray) difference = cv2.absdiff( self.bg_model.background.astype("uint8")[y:y + h, x:x + w], gray[y:y + h, x:x + w]) foreground = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)[1] gest, frame[y:y + h, x:x + w] = self.gesture_recognizer.recognize(foreground) self.last_gest = str(gest) except: pass return (frame, username, gesture)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) gray = cv2.GaussianBlur(gray, (9, 9), 0) if self.num_frames < 30: if self.background is None: self.background = gray.copy().astype("float") cv2.accumulateWeighted(gray, self.background, 0.5) self.num_frames += 1 else: self.calibrated = True if __name__ == "__main__": rec = Recognition() db = DBHelper() cam = WebcamVideoStream(src=0).start() user = "" response = input("Register new user? y or n \n") if response == 'y': rec.is_registering = True user = input("Enter a username: "******"", "", "", "", ""]) else: rec.is_registering = False while (True): frame = cam.read() frame = cv2.resize(frame, (640, 480)) out, user, gest = rec.processFrame(frame, user) cv2.imshow("out", out)
keyboard = [[InlineKeyboardButton("List Events", callback_data=str(LIST))], [InlineKeyboardButton("Add Event", callback_data=str(ADD))], [ InlineKeyboardButton("Delete Event", callback_data=str(DELETE)) ], [InlineKeyboardButton("Cancel", callback_data=str(CANCEL))]] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text('Please choose:', reply_markup=reply_markup) return EVENTM # query = update.callback_query # logger.info("This is your buttons callback_data:{}".format(query.data)) from database import DBHelper db = DBHelper() def add_event(update, context): # text_start = 'Hello\! I\'m mycrowbot\.\nHope you\'re doing good\!\n'\ # 'Check /help to know what I can do' # context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='MarkdownV2', text=text_start) add_instructions = 'To add an event, send me the event details in the following format:\n'\ 'First, send me the name as one message, then send the description as another\n'\ 'To cancel this operation, send /cancel\n'\ 'Example:'\ format_name = 'Crowbot Hackathon'\ format_desc = '19th March 2002, 8:00AM\n'\ 'Venue: Lab 2, Somebuildingname, Andheri West\n'\
import requests import time import datetime as DT from database import DBHelper from matplotlib import pyplot as plt import pandas as pd import random from config import CONFIG import re import os CURRENCY_FROM = 0 AMOUNT_FROM = 1 CURRENCY_TO = -1 CURRENCY_PERIOD = 2 db = DBHelper(CONFIG["db_name"]) def get_list_url(): r = requests.get('https://api.exchangeratesapi.io/latest?base=USD') r.raise_for_status() content = r.json() return content['rates'] def get_history_url(period, symbols): today = DT.date.today() end_at = today.strftime("%Y-%m-%d") start_at = (today - DT.timedelta(days=period)).strftime("%Y-%m-%d") r = requests.get( 'https://api.exchangeratesapi.io/history?start_at={0}&end_at={1}&base=USD&symbols={2}'