Last active
July 8, 2019 06:18
-
-
Save rushabhnagda11/c6d194a2d06c4257e5690c66025d8535 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import cv2 | |
| import json | |
| url = 'https://app.nanonets.com/api/v2/ObjectDetection/Model/REPLACE_MODEL_ID/LabelFile/' | |
| #-----GET CAMERA FEED---- | |
| cap = cv2.VideoCapture(0) | |
| # Check if the drone feed is opened correctly | |
| if not cap.isOpened(): | |
| raise IOError("Cannot connect to drone feed.") | |
| ret, frame = cap.read() | |
| height, width, _ = frame.shape | |
| while True: | |
| #-----GET WEBCAM FRAME---- | |
| ret, frame = cap.read() | |
| #-----DO SOMETHING----- | |
| _, img_encoded = cv2.imencode('.jpg', frame) | |
| response = requests.post( | |
| url, auth=requests.auth.HTTPBasicAuth('YOUR_API_KEY', ''), | |
| files={"file": ("frame.jpg", img_encoded.tostring())}, | |
| ) | |
| response = json.loads(response.text) | |
| try: | |
| prediction = response[0]["prediction"] | |
| for i in prediction: | |
| frame = cv2.rectangle(frame,(i['xmin'],i['ymin']),(i['xmax'],i['ymax']),(0,0,255),2) | |
| except Exception as e: | |
| pass | |
| #-----END OF DO SOMETHING----- | |
| cv2.imshow('nanonets', frame) | |
| # Press esc to exit/stop | |
| c = cv2.waitKey(1) | |
| if c == 27: | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() | |
| cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment