<jupyter_start><jupyter_text>Coefficient - run this first!<jupyter_code>import numpy as np
dist = np.array([-0.53115401, 0.49380163, 0.02334731, 0.01249862, -0.19562481])
mtx = np.array([[599.78448344, 0,             331.4887726 ],
                [0,            595.3968694,   131.26905818],
                [0,            0,                        1]])
newcameramtx = np.array([[487.06121826,  0,            347.03125155],
                         [0,             476.48925781, 142.04030917],
                         [0,             0,            1           ]])
roi = (19,32,608,302)<jupyter_output><empty_output><jupyter_text>What you need to notice?1. If the drone wont connect, just restart the kernel. The problem is the kernel2. Dont use function for 2 drone! Content:1. Live streaming - 1 drone2. Live streaming - 2 drone3. Live streaming with undistorted image!4. Save video5. Save video with undistorted image - 1 drone!6. Save video with undistorted image - 2 drone! (run this for final result!)7. Drone capturing the image!8. Calibration  Live streaming - 1 drone<jupyter_code>import cv2
cam = cv2.VideoCapture("tcp://192.168.1.1:5555")
running = True
while running:
    running, frame = cam.read()
    if running:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == 27: #0xFF is representing binary. more detail:
            # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1
            running = False
    else:
        #error reading frame
        print ('error reading video feed')

cam.release() #Closes video file or capturing device.
cv2.destroyAllWindows() #used to force all the open windows to close at once.
# https://stackoverflow.com/questions/37713155/stream-video-from-drone-parrot-2-0-python-cv2<jupyter_output><empty_output><jupyter_text>Live streaming - 2 drone<jupyter_code>import cv2
cam = cv2.VideoCapture("tcp://192.168.1.9:5555")
cam2 = cv2.VideoCapture("tcp://192.168.1.2:5555")
running = True
running2 = True
while running:
    running, frame = cam.read()
    running2, frame2 = cam2.read()
    if running2:
        cv2.imshow('frame2', frame2)
        if cv2.waitKey(1) & 0xFF == 27: #0xFF is representing binary. more detail:
            # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1
            running2 = False
    else:
        #error reading frame
        print ('error reading video1 feed')
        
    if running:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == 27: #0xFF is representing binary. more detail:
            # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1
            running = False
    else:
        #error reading frame
        print ('error reading video2 feed')

cam.release() #Closes video file or capturing device.
cam2.release() #Closes video file or capturing device.
cv2.destroyAllWindows() #used to force all the open windows to close at once.
# https://stackoverflow.com/questions/37713155/stream-video-from-drone-parrot-2-0-python-cv2<jupyter_output><empty_output><jupyter_text>Live streaming with undistorted image!<jupyter_code>import cv2

cam = cv2.VideoCapture("tcp://192.168.1.2:5555")
running = True
while running:
    running, frame = cam.read()
    if running:
        # undistort
        dst = cv2.undistort(frame, mtx, dist, None, newcameramtx)
        x, y, w, h = roi
        dst = dst[y:y+h, x:x+w]
        cv2.imshow('asd', dst)
        if cv2.waitKey(1) & 0xFF == 27: 
            # escape key pressed 
            running = False
    else:
        # error reading frame
        print ('error reading video feed')

cam.release()
cv2.destroyAllWindows()

# https://stackoverflow.com/questions/37713155/stream-video-from-drone-parrot-2-0-python-cv2<jupyter_output><empty_output><jupyter_text>Save video<jupyter_code>import cv2
  
# Create an object to read 
# from camera
video = cv2.VideoCapture("tcp://192.168.1.9:5555")
   
# We need to check if camera
# is opened previously or not
if (video.isOpened() == False): 
    print("Error reading video file")

# We need to set resolutions.
# so, convert them from float to integer.
frame_width = int(video.get(3)) #Width of the frames in the video stream.
frame_height = int(video.get(4)) #Height of the frames in the video stream.
   
size = (frame_width, frame_height)
   
# Below VideoWriter object will create
# a frame of above defined The output 
# is stored in 'filename.avi' file.
result = cv2.VideoWriter('filename30.avi', 
                         cv2.VideoWriter_fourcc(*'MJPG'),
                         30, size)
    
while(True):
    ret, frame = video.read()
  
    if ret == True: 
  
        # Write the frame into the
        # file 'filename.avi'
        result.write(frame)
  
        # Display the frame
        # saved in the file
        cv2.imshow('Frame', frame)
  
        # Press S on keyboard 
        # to stop the process
        if cv2.waitKey(1) & 0xFF == ord('s'):
            break
  
    # Break the loop
    else:
        break

# When everything done, release 
# the video capture and video 
# write objects
video.release()
result.release()
    
# Closes all the frames
cv2.destroyAllWindows()
   
print("The video was successfully saved")<jupyter_output>The video was successfully saved<jupyter_text>Save video with undistorted image - 1 drone!<jupyter_code>import cv2
cam = cv2.VideoCapture("tcp://192.168.1.2:5555")
frame_width = int(cam.get(3)) #Width of the frames in the video stream.
frame_height = int(cam.get(4)) #Height of the frames in the video stream.
size = (frame_width, frame_height)
result = cv2.VideoWriter('filetry9.avi', cv2.VideoWriter_fourcc(*'MJPG'), 30, size) 
#(fileoutput, code of codec used to compress the frames, fps, size of the videos)
running = True
while running:
    running, frame = cam.read()
    if running:
        # undistort
        dst = cv2.undistort(frame, mtx, dist, None, newcameramtx)
        #you cannot crop this image! this is too hard to get done! 
        #x, y, w, h = roi
        #dst = dst[y:y+h, x:x+w]
        #size = (h, w)
        #result = cv2.VideoWriter('filename2.avi', cv2.VideoWriter_fourcc(*'MJPG'), 10, size) 
        result.write(dst)
        cv2.imshow('frame', dst)
        if cv2.waitKey(1) & 0xFF == 27: 
            # escape key pressed 
            running = False
    else:
        # error reading frame
        print ('error reading video feed')

cam.release()
cv2.destroyAllWindows()

# https://stackoverflow.com/questions/37713155/stream-video-from-drone-parrot-2-0-python-cv2<jupyter_output><empty_output><jupyter_text>Save video with undistorted image - 2 drone!<jupyter_code>import cv2 

#variable
ip1 = "tcp://192.168.1.1:5555"
video1 = "file1.avi"
ip2 = "tcp://192.168.2.2:5555"
video2 = "file2.avi"
running1 = True
running2 = True

#drone 1
cam1 = cv2.VideoCapture(ip1)
frame_width = int(cam1.get(3))
frame_height = int(cam1.get(4))
size1 = (frame_width, frame_height)
result1 = cv2.VideoWriter(video1, cv2.VideoWriter_fourcc(*'MJPG'), 30, size1) 

#drone2
cam2 = cv2.VideoCapture(ip2)
frame_width = int(cam2.get(3))
frame_height = int(cam2.get(4))
size2 = (frame_width, frame_height)
result2 = cv2.VideoWriter(video2, cv2.VideoWriter_fourcc(*'MJPG'), 30, size2) 

while running1:
    running1, frame1 = cam1.read()
    running2, frame2 = cam2.read()
    
    #drone 1
    if running1:
        dst1 = cv2.undistort(frame1, mtx, dist, None, newcameramtx)
        result1.write(dst1)
        cv2.imshow('frame1', dst1)
        if cv2.waitKey(1) & 0xFF == 27:  
            running1 = False
    else:
        print ('error reading video1 feed')
        
    #drone 2
    if running2:
        dst2 = cv2.undistort(frame2, mtx, dist, None, newcameramtx)
        result2.write(dst2)
        cv2.imshow('frame2', dst2)
        if cv2.waitKey(1) & 0xFF == 27: 
            running2 = False
    else:
        print ('error reading video2 feed')

#shut all down
cam1.release()
cam2.release()
cv2.destroyAllWindows()<jupyter_output>error reading video2 feed
error reading video2 feed
error reading video2 feed<jupyter_text>Drone capturing the image!<jupyter_code>import cv2

cam = cv2.VideoCapture("tcp://192.168.1.1:5555")

cv2.namedWindow("test")

img_counter = 41

while True:
    ret, frame = cam.read()
    dst = cv2.undistort(frame, mtx, dist, None, newcameramtx)
    if not ret:
        print("failed to grab frame")
        break
    cv2.imshow("test", dst)

    k = cv2.waitKey(1)
    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")
        break
    elif k%256 == 32:
        # SPACE pressed
        img_name = "image{}.png".format(img_counter)
        cv2.imwrite(img_name, dst)
        print("{} written!".format(img_name))
        img_counter += 1 

cam.release()

cv2.destroyAllWindows()<jupyter_output>image0.png written!
image1.png written!
image2.png written!
image3.png written!
image4.png written!
image5.png written!
image6.png written!
image7.png written!
image8.png written!
image9.png written!
image10.png written!
image11.png written!
image12.png written!
image13.png written!
image14.png written!
image15.png written!
image16.png written!
image17.png written!
image18.png written!
image19.png written!
image20.png written!
image21.png written!
image22.png written!
image23.png written!
image24.png written!
image25.png written!
image26.png written!
image27.png written!
image28.png written!
image29.png written!
image30.png written!
image31.png written!
image32.png written!
image33.png written!
image34.png written!
image35.png written!
image36.png written!
image37.png written!
image38.png written!
image39.png written!
image40.png written!<jupyter_text>Calibration<jupyter_code>import numpy as np
import cv2
import glob
import argparse

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

def calibrate(dirpath, prefix, image_format, square_size, width=9, height=6):
    """ Apply camera calibration operation for images in the given directory path. """
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(8,6,0)
    objp = np.zeros((height*width, 3), np.float32)
    objp[:, :2] = np.mgrid[0:width, 0:height].T.reshape(-1, 2)

    objp = objp * square_size

    # Arrays to store object points and image points from all the images.
    objpoints = []  # 3d point in real world space
    imgpoints = []  # 2d points in image plane.

    #if dirpath[-1:] == '/':
    #    dirpath = dirpath[:-1]

    #images = glob.glob(dirpath+'/' + prefix + '*.' + image_format)
    images = glob.glob('*.png')
    for fname in images:
        img = cv2.imread(fname)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Find the chess board corners
        ret, corners = cv2.findChessboardCorners(gray, (width, height), None)

        # If found, add object points, image points (after refining them)
        if ret:
            objpoints.append(objp)

            corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
            imgpoints.append(corners2)

            # Draw and display the corners
            img = cv2.drawChessboardCorners(img, (width, height), corners2, ret)

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

    return [ret, mtx, dist, rvecs, tvecs]

def save_coefficients(mtx, dist, path):
    """ Save the camera matrix and the distortion coefficients to given path/file. """
    cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_WRITE)
    cv_file.write("K", mtx)
    cv_file.write("D", dist)
    # note you *release* you don't close() a FileStorage object
    cv_file.release()
def load_coefficients(path):
    """ Loads camera matrix and distortion coefficients. """
    # FILE_STORAGE_READ
    cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_READ)

    # note we also have to specify the type to retrieve other wise we only get a
    # FileNode object back instead of a matrix
    camera_matrix = cv_file.getNode("K").mat()
    dist_matrix = cv_file.getNode("D").mat()

    cv_file.release()
    return [camera_matrix, dist_matrix]
ret, mtx, dist, rvecs, tvecs = calibrate(r'C:\\Users\\halim\\image_drone', 'image', '.png', 0.0285, 9, 6)
save_coefficients(mtx, dist, "C:\\Users\\halim\\image_drone")
print("Calibration is finished. RMS: ", ret)
%pwd
%cd image_drone
#note that you should in this folder: C:\Users\halim\image_drone

import numpy as np
import cv2 as cv
import glob
# termination criteria, you could read more from this documentation: 
# https://docs.opencv.org/master/d1/d5c/tutorial_py_kmeans_opencv.html
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32) # np.float32 is dtype
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2) # print the statement to know what is the list look like!

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('*.png')

for fname in images:
    img = cv.imread(fname)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    
    # Find the chess board corners
    ret, corners = cv.findChessboardCorners(gray, (9,6), None) # ret return true/false, corners return array of detected corner!
    
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)
        corners2 = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)
        # Draw and display the corners
        cv.drawChessboardCorners(img, (9,6), corners2, ret)
        cv.imshow('img', img)
        cv.waitKey(100)
        
cv.destroyAllWindows()
ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 
# '::' is slice operator or double colon
# mtx: camera matrix
# dist: distortion coefficient
# rvecs: Rotation specified as a 3×1 vector. The direction of the vector specifies the axis of rotation and 
# the magnitude of the vector specifies the angle of rotation.
# tvecs: 3×1 Translation vector.
img = cv.imread('image14.png')
h,  w = img.shape[:2]
newcameramtx, roi = cv.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
print(newcameramtx)
type(newcameramtx)
# undistort
dst = cv.undistort(img, mtx, dist, None, newcameramtx)

# crop the image
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite('calibresult14.png', dst)
print(mtx)
type(mtx)
print(dist)
type(dist)
arr = np.array([[1, 2, 3, 4, 5],
               [1, 2, 3, 4, 6]])
print(arr)
type(mtx)
print (rvecs)
import requests
import cv2
import numpy as np
import imutils
  
# Replace the below URL with your own. Make sure to add "/shot.jpg" at last.
url = "http://192.168.0.103:8080/shot.jpg"
  
# While loop to continuously fetching data from the Url
while True:
    img_resp = requests.get(url)
    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
    img = cv2.imdecode(img_arr, -1)
    img = imutils.resize(img, width=1000, height=1800)
    cv2.imshow("Android_cam", img)
  
    # Press Esc key to exit
    if cv2.waitKey(1) == 27:
        break
  
cv2.destroyAllWindows()
a = ['1','2','3']

for x in a:
    print(x)
class Obstacle(object):
    def __init__(self, position):
        self.position = position
        self.positionHistory = [position]
        self.ObstaclePotentialForces = []

Obstacles = []
Coral = Obstacle((0, 60, 50))
Coral2 = Obstacle((0, 50, 50))
Obstacles.append(Coral)
Obstacles.append(Coral2)

print(Obstacles)
position = ['1','2','3']
history = [position]
print(history)
position = ['3','3','3']
history = [position]
print(history)
Drone1 = (0, (0, 5, 4), 1)
Drone1.apf = "1"
print (Drone1.apf)
import cv2
thres = 0.45 # Threshold to detect object

cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
cap.set(10,70)

classNames= []
classFile = 'coco.names'
with open(classFile,'rt') as f:
    classNames=[line.rstrip() for line in f]

configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)

while True:
    success,img = cap.read()
    classIds, confs, bbox = net.detect(img,confThreshold=thres)
    print(classIds,bbox)

    if len(classIds) != 0:
        for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
            cv2.rectangle(img,box,color=(0,255,0),thickness=2)
            cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
            cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
            cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
            cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)

    cv2.imshow("Output",img)
    cv2.waitKey(1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12751677852348994, 'fraction_numerical': 0.05439773931472978, 'mean_word_length': 3.231938216243149, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '545240', 'license': 'MIT', 'n_tokens_mistral': 6410, 'n_tokens_neox': 5928, 'n_words': 1532, 'path': 'Object Detection/Code for video!.ipynb', 'repo_name': 'cholazzzb/swarm-parrot2', 'size': '16986'}