Need a graphical user interface made on iOS using swift with codes

profileahsanagwan
find_shot2.py

# import the necessary packages from __future__ import print_function from skimage.measure import compare_ssim from skimage.transform import resize import argparse import imutils import cv2 import statistics import numpy as np MAX_FEATURES = 500 GOOD_MATCH_PERCENT = 0.18 wt = 95 ht = 145 def find_crosshairs(img): #img = cv2.imread(, 0) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.resize(img, (560, 1020), interpolation=cv2.INTER_NEAREST) img = cv2.GaussianBlur(img, (5, 5), 0) #img = cv2.medianBlur(img, 5) cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=20, maxRadius=50) circles = np.uint16(np.around(circles)) list_of_centers = [] list_of_centers2 = [] x_coor = [] n = 0 for i in circles[0, :]: # draw the outer circle if i[0] > 60 and i[0] < 500 and i[1]>100 and i[1] < 800: cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2) x_center, y_center = i[0], i[1] x_coor.append(i[0]) crosshair_center = tuple([x_center, y_center]) if n == 0: list_of_centers.append(crosshair_center) pix_per_inch = i[2] * 2.2 / 4 radius = i[2] # cv2.circle(image, crosshair_center, 35, (0, 255, 255), 2) elif n >= 1: #and x_coor[n - 1] < x_coor[n] - 50 or x_coor[n - 1] > x_coor[ #n] + 50: # filters our similar coordinates list_of_centers.append(crosshair_center) list_of_centers2.append(crosshair_center) # cv2.circle(image, crosshair_center, 35, (0, 255, 255), 2) n = n + 1 # draw the center of the circle cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3) #print(circles) cv2.imshow('detected circles', cimg) cv2.waitKey(0) return list_of_centers, pix_per_inch, radius def alignImages(imB, imA): # Convert images to grayscale im1Gray = cv2.cvtColor(imB, cv2.COLOR_BGR2GRAY) im2Gray = cv2.cvtColor(imA, cv2.COLOR_BGR2GRAY) # Detect ORB features and compute descriptors. orb = cv2.ORB_create(MAX_FEATURES) keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None) keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None) # Match features. matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING) matches = matcher.match(descriptors1, descriptors2, None) # Sort matches by score matches.sort(key=lambda x: x.distance, reverse=False) # Remove not so good matches numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT) matches = matches[:numGoodMatches] # Draw top matches imMatches = cv2.drawMatches(imB, keypoints1, imA, keypoints2, matches, None) cv2.imwrite("matches.jpg", imMatches) # Extract location of good matches points1 = np.zeros((len(matches), 2), dtype=np.float32) points2 = np.zeros((len(matches), 2), dtype=np.float32) for i, match in enumerate(matches): points1[i, :] = keypoints1[match.queryIdx].pt points2[i, :] = keypoints2[match.trainIdx].pt # Find homography h, mask = cv2.findHomography(points1, points2, cv2.RANSAC) # Use homography height, width, channels = imA.shape im1Reg = cv2.warpPerspective(imB, h, (width, height)) return im1Reg, h def resizeImages(IMA, IMB, r): #r does NOTHING #find size of images heightA, widthA, channelA = IMA.shape heightB, widthB, channelB = IMB.shape #print(heightA,widthA) #resize image 1 heightA = int(heightA/ r) widthA = int(widthA/ r) IMA= cv2.resize(IMA, (560, 1020), interpolation = cv2.INTER_NEAREST) #resize image2 IMB = cv2.resize(IMB, (560, 1020), interpolation = cv2.INTER_NEAREST) return IMA, IMB #The program will keep running while again is 1 again =1 first =1 # construct the argument parse and parse the arguments while again == 1: if first ==1: ap = argparse.ArgumentParser() ap.add_argument("-f", "--first", required=True, help="first input image") ap.add_argument("-s", "--second", required=True, help="second") args = vars(ap.parse_args()) imageA = cv2.imread(args["first"]) imageB = cv2.imread(args["second"]) copy_B = imageB.copy() distance_to_target_center_list = [] elif first == 0: img_name = input("input new image name: ") imageB = cv2.imread(img_name) imageA = last_image.copy() copy_B = imageB.copy() imageB, h = alignImages(imageB, imageA) imageA, imageB = resizeImages(imageA, imageB, 4) #Align and Resize Images multiple times for accuracy times = 5 i=1 while i < times: imageB , h = alignImages(imageB, imageA) i +=1 #find targets and their coordinates imageC = imageA.copy() crosshair_center = [] crosshair_center, pixelsperinch, radius = find_crosshairs(imageC) #print(crosshair_center) i=1 for xy in crosshair_center: cv2.circle(imageC, xy, radius, (0, 255, 255), 2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(imageC, str(i), xy, font, 1, (200, 255, 155), 2, cv2.LINE_AA) i=i+1 #receive input of which target user will be shooting at cv2.imshow('Pick a Target: ', imageC) cv2.waitKey(0) target = input('Which target will you be shooting at?: ') target = int(target) - 1 x_crosshair = crosshair_center[target][0] y_crosshair = crosshair_center[target][1] # convert the images to grayscale grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY) grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY) # compute the Structural Similarity Index (SSIM) between the two # images, ensuring that the difference image is returned (score, diff) = compare_ssim(grayA, grayB, full=True) diff = (diff * 255).astype("uint8") #print("SSIM: {}".format(score)) # threshold the difference image, followed by finding contours to # obtain the regions of the two input images that differ thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) #find avg area of found differences in order to use for filtering effects areas = [] for c in cnts: (x, y, w, h) = cv2.boundingRect(c) wa, ha, c = imageA.shape picture_area = wa*ha area = w*h if area < picture_area/4 : #this filters out large area differences areas.append(area) # loop over the contours i=0 avg_area = sum(areas) / (len(areas)+1) #avg area of differences (not including large differences) for c in cnts: # compute the bounding box of the contour and then draw the # bounding box on both input images to represent where the two # images differ (x, y, w, h) = cv2.boundingRect(c) #we need to filter out some differences area = w*h heightA, widthA, channelA = imageA.shape x_center = widthA / 2 y_center = heightA / 2 if abs(x-x_crosshair) * pixelsperinch**-1 < 4 and abs(y_crosshair-y) * pixelsperinch**-1 < 4: #filters out differences far away from target if area > avg_area/2 and area < picture_area/10: #this filters out the micro differences and more large differences area > avg_area/2 and if w < h*1.4 and w > h*.6: #this filters out irregular shapes (non-squares) if x > 0 + widthA/8 and x < widthA-widthA/8 and y > 0 + heightA/8 and y < heightA-heightA/8: #filters out edges i=i+1 cv2.circle(imageA, (int(x+w/2),int(y+h/2)), 3, (0, 0, 255), 2) cv2.circle(imageB, (int(x+w/2),int(y+h/2)), 3, (0, 0, 255), 2) shot_coor_x = x shot_coor_y = y #calculate the horizontal and vertical distance between our shot and the target center horizontal_diff = (x+w/2 - x_crosshair) * pixelsperinch**-1 #positive means right vertical_diff = (y_crosshair - y+h/2) * pixelsperinch**-1 #positive means high distance_to_target_center = (horizontal_diff**2 + vertical_diff**2) ** (1/2) #ditance from shot to target in inches distance_to_target_center_list.append(distance_to_target_center) #create list of distances cv2.line(imageB, (int(x+w/2), int(y+h/2)), (crosshair_center[target]), (0, 255, 0), 2) if horizontal_diff <= 0: print('Hit too left by: ' + str(abs(horizontal_diff) ) + ' in') elif horizontal_diff > 0: print('Hit too right by: ' + str(horizontal_diff) + ' in') if vertical_diff > 0: print('Hit to high by: ' + str(vertical_diff) + ' in') elif vertical_diff < 0: print('Hit too low by: ' + str(abs(vertical_diff)) + ' in') print('distance from center of target (in) ' + str(distance_to_target_center)) i = str(i) for xy in crosshair_center: cv2.circle(imageB, (x_crosshair, y_crosshair), radius, (0, 255, 255), 2) #print('number of matches: ' + i) # show the output images avg_distance = sum(distance_to_target_center_list) / len(distance_to_target_center_list) if len(distance_to_target_center_list) != 1: std_dev = statistics.stdev(distance_to_target_center_list) print('The average distance from the center of the target is: ' + str( avg_distance) + ' with a standard deviation of: ' + str(std_dev)) cv2.imshow("Original", imageA) cv2.imshow("Modified", imageB) cv2.imshow("Diff", diff) cv2.imshow("Thresh", thresh) cv2.waitKey(0) again = input('WIll you shoot again yes(1), no (2): ') again = int(again) first =0; last_image = copy_B.copy();