Jassk28 commited on
Commit
a490efd
·
1 Parent(s): 6e70b24

Upload utlis.py

Browse files
Files changed (1) hide show
  1. utlis.py +124 -0
utlis.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ ## TO STACK ALL THE IMAGES IN ONE WINDOW
5
+ def stackImages(imgArray,scale,lables=[]):
6
+ rows = len(imgArray)
7
+ cols = len(imgArray[0])
8
+ rowsAvailable = isinstance(imgArray[0], list)
9
+ width = imgArray[0][0].shape[1]
10
+ height = imgArray[0][0].shape[0]
11
+ if rowsAvailable:
12
+ for x in range ( 0, rows):
13
+ for y in range(0, cols):
14
+ imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
15
+ if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
16
+ imageBlank = np.zeros((height, width, 3), np.uint8)
17
+ hor = [imageBlank]*rows
18
+ hor_con = [imageBlank]*rows
19
+ for x in range(0, rows):
20
+ hor[x] = np.hstack(imgArray[x])
21
+ hor_con[x] = np.concatenate(imgArray[x])
22
+ ver = np.vstack(hor)
23
+ ver_con = np.concatenate(hor)
24
+ else:
25
+ for x in range(0, rows):
26
+ imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
27
+ if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
28
+ hor= np.hstack(imgArray)
29
+ hor_con= np.concatenate(imgArray)
30
+ ver = hor
31
+ if len(lables) != 0:
32
+ eachImgWidth= int(ver.shape[1] / cols)
33
+ eachImgHeight = int(ver.shape[0] / rows)
34
+ #print(eachImgHeight)
35
+ for d in range(0, rows):
36
+ for c in range (0,cols):
37
+ cv2.rectangle(ver,(c*eachImgWidth,eachImgHeight*d),(c*eachImgWidth+len(lables[d][c])*13+27,30+eachImgHeight*d),(255,255,255),cv2.FILLED)
38
+ cv2.putText(ver,lables[d][c],(eachImgWidth*c+10,eachImgHeight*d+20),cv2.FONT_HERSHEY_COMPLEX,0.7,(255,0,255),2)
39
+ return ver
40
+
41
+ def reorder(myPoints):
42
+
43
+ myPoints = myPoints.reshape((4, 2)) # REMOVE EXTRA BRACKET
44
+ print(myPoints)
45
+ myPointsNew = np.zeros((4, 1, 2), np.int32) # NEW MATRIX WITH ARRANGED POINTS
46
+ add = myPoints.sum(1)
47
+ print(add)
48
+ print(np.argmax(add))
49
+ myPointsNew[0] = myPoints[np.argmin(add)] #[0,0]
50
+ myPointsNew[3] =myPoints[np.argmax(add)] #[w,h]
51
+ diff = np.diff(myPoints, axis=1)
52
+ myPointsNew[1] =myPoints[np.argmin(diff)] #[w,0]
53
+ myPointsNew[2] = myPoints[np.argmax(diff)] #[h,0]
54
+
55
+ return myPointsNew
56
+
57
+ def rectContour(contours):
58
+
59
+ rectCon = []
60
+ max_area = 0
61
+ for i in contours:
62
+ area = cv2.contourArea(i)
63
+ if area > 50:
64
+ peri = cv2.arcLength(i, True)
65
+ approx = cv2.approxPolyDP(i, 0.02 * peri, True)
66
+ if len(approx) == 4:
67
+ rectCon.append(i)
68
+ rectCon = sorted(rectCon, key=cv2.contourArea,reverse=True)
69
+ #print(len(rectCon))
70
+ return rectCon
71
+
72
+ def getCornerPoints(cont):
73
+ peri = cv2.arcLength(cont, True) # LENGTH OF CONTOUR
74
+ approx = cv2.approxPolyDP(cont, 0.02 * peri, True) # APPROXIMATE THE POLY TO GET CORNER POINTS
75
+ return approx
76
+
77
+ def splitBoxes(img):
78
+ rows = np.vsplit(img,5)
79
+ boxes=[]
80
+ for r in rows:
81
+ cols= np.hsplit(r,5)
82
+ for box in cols:
83
+ boxes.append(box)
84
+ return boxes
85
+
86
+ def drawGrid(img,questions=5,choices=5):
87
+ secW = int(img.shape[1]/questions)
88
+ secH = int(img.shape[0]/choices)
89
+ for i in range (0,9):
90
+ pt1 = (0,secH*i)
91
+ pt2 = (img.shape[1],secH*i)
92
+ pt3 = (secW * i, 0)
93
+ pt4 = (secW*i,img.shape[0])
94
+ cv2.line(img, pt1, pt2, (255, 255, 0),2)
95
+ cv2.line(img, pt3, pt4, (255, 255, 0),2)
96
+
97
+ return img
98
+
99
+ def showAnswers(img,myIndex,grading,ans,questions=5,choices=5):
100
+ secW = int(img.shape[1]/questions)
101
+ secH = int(img.shape[0]/choices)
102
+
103
+ for x in range(0,questions):
104
+ myAns= myIndex[x]
105
+ cX = (myAns * secW) + secW // 2
106
+ cY = (x * secH) + secH // 2
107
+ if grading[x]==1:
108
+ myColor = (0,255,0)
109
+ #cv2.rectangle(img,(myAns*secW,x*secH),((myAns*secW)+secW,(x*secH)+secH),myColor,cv2.FILLED)
110
+ cv2.circle(img,(cX,cY),50,myColor,cv2.FILLED)
111
+ else:
112
+ myColor = (0,0,255)
113
+ #cv2.rectangle(img, (myAns * secW, x * secH), ((myAns * secW) + secW, (x * secH) + secH), myColor, cv2.FILLED)
114
+ cv2.circle(img, (cX, cY), 50, myColor, cv2.FILLED)
115
+
116
+ # CORRECT ANSWER
117
+ myColor = (0, 255, 0)
118
+ correctAns = ans[x]
119
+ cv2.circle(img,((correctAns * secW)+secW//2, (x * secH)+secH//2),
120
+ 20,myColor,cv2.FILLED)
121
+
122
+
123
+
124
+