File size: 19,957 Bytes
154e666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# -*- coding: utf-8 -*-
"""3.2(Ready for new interface).ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/16maX93rvCuU14RBPDK60YxXqxv7aB4Jz

# Libraries
"""

# from google.colab.patches import cv2_imshow
import cv2
import numpy as np
import pandas as pd

import statistics
from statistics import mode

from PIL import Image

# !pip install easydev
# !pip install colormap
# !pip install extcolors

import matplotlib.pyplot as plt
import matplotlib.patches as patches

import extcolors

from colormap import rgb2hex

# !pip install pypdfium2

import pypdfium2 as pdfium
import math
import fitz
import db
import pilecaps_adr
"""# Reading Pdf file and returning image ready to use

"""

def convert2img(path):
    pdf = pdfium.PdfDocument(path)
    page = pdf.get_page(0)
    pil_image = page.render().to_pil()
    pl1=np.array(pil_image)
    img = cv2.cvtColor(pl1, cv2.COLOR_RGB2BGR)
    return img

def readImgggg(img):
  hsv  = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  return hsv

#change Background to red, this step before changing the gray shapes
#This function helps in improving colour extraction later in the code
def changeBackground(img):
  imgCopy = img.copy()
  hsv = cv2.cvtColor(imgCopy, cv2.COLOR_BGR2HSV)
  white_range_low = np.array([0,0,250])
  white_range_high = np.array([0,0,255])
  mask2=cv2.inRange(hsv,white_range_low, white_range_high)
  imgCopy[mask2>0]=(0,0,255)
  return imgCopy

#If there any gray shapes it will be changed to blue
def changeGrayModify(img):
  noGray = changeBackground(img)
  hsv = cv2.cvtColor(noGray, cv2.COLOR_BGR2HSV)
  gray_range_low = np.array([0,0,170])
  gray_range_high = np.array([150,30,255])
  mask=cv2.inRange(hsv,gray_range_low,gray_range_high)
  noGray[mask>0]=(255,0,0)
  return noGray

#We may use this function to return background to white after changing the gray shapes to blue
def returnWhite(img):
  imgCopy = img.copy()
  hsv = cv2.cvtColor(imgCopy, cv2.COLOR_BGR2HSV)
  red_range_low = np.array([0,250,250])
  red_range_high = np.array([0,255,255])
  mask2=cv2.inRange(hsv,red_range_low, red_range_high)
  imgCopy[mask2>0]=(255,255,255)
  return imgCopy

"""# Extracting colours for the next phase (preprocessing)"""

#This function will take the image with red background and extract all the colours in the plan
def extractClrs(medianF):
  im = cv2.cvtColor(medianF, cv2.COLOR_BGR2RGB)
  im = Image.fromarray(im)
  colors_x = extcolors.extract_from_image(im,8,8)
  listofTuples = []
  for i in range(len(colors_x[0])):
    listofTuples.append(colors_x[0][i][0])
  rgb = []
  for i in range(len(listofTuples)):
    rgb.append([])
  for i in range(len(listofTuples)):
    for j in range(len(listofTuples[i])):
      rgb[i].append(listofTuples[i][j])
  rgb = np.uint8([[rgb]])
  clr_hsv = []
  for i in range(len(rgb)):
    clr_hsv.append(cv2.cvtColor(rgb[i], cv2.COLOR_RGB2HSV))
  return clr_hsv

#Used to delete any gray or white colours detected
def deleteZeroHue(clr_hsv):
  clr_hsv_short = []
  for i in range(len(clr_hsv[0][0])):
    if clr_hsv[0][0][i][0] > 0:
      clr_hsv_short.append(clr_hsv[0][0][i])
  return clr_hsv_short

#Saving the region of the different colours in the plan
def dividingInput(clr_hsv_short):
  hInput = []
  sInput = []
  vInput = []
  for i in range(len(clr_hsv_short)):
    hInput.append(clr_hsv_short[i][0])
    sInput.append(clr_hsv_short[i][1])
    vInput.append(clr_hsv_short[i][2])
  return hInput, sInput, vInput

#Setting the range of upper and lower range of the Hue of different colours
#Will be used to separate each shape inside the plan in a different img due to their different colours
def setRange(n, hInput):
  lower = []
  upper = []
  for i in range(len(hInput)):
    if (hInput[i] - n > 0):
      lower.append(np.uint(hInput[i] - n))
    else:
      lower.append(np.uint(hInput[i]))
    upper.append(np.uint(hInput[i] + n))
  return lower, upper

"""# Reading the image's colours"""

#Saving all the hues, saturations, values of the plan
def getColoursImage(hsv, img):
  hue = hsv[:,:,0]
  saturation = hsv[:,:,1]
  value = hsv[:,:,2]
  h=[]
  s=[]
  v=[]

  for i in range(img.shape[1]):
    for j in range(img.shape[0]):
      if hue[j][i] > 0 :
        h.append(hue[j][i])
        s.append(saturation[j][i])
        v.append(value[j][i])
  return h,s,v

#Putting different hues in different categories (each colour is a category)
def categorizeV4(hsv, lower, upper, img):
  h, s, v = getColoursImage(hsv, img)
  groups = []
  for i in range(len(lower)):
    groups.append([])
  for i in range(len(h)):
    for j in range(len(lower)):
      if h[i] in range(lower[j],upper[j]):
        groups[j].append([h[i], s[i], v[i]])
  return groups

#Putting the hue, saturation, values in the categories
def getHuesV3(categorey):
  hues = []
  for i in range(len(categorey)):
    hues.append([])
  for i in range(len(categorey)):
    for j in range(len(categorey[i])):
      hues[i].append(categorey[i][j][0])
  return hues

def getSaturationsV3(categorey):
  saturations = []
  for i in range(len(categorey)):
    saturations.append([])
  for i in range(len(categorey)):
    for j in range(len(categorey[i])):
      saturations[i].append(categorey[i][j][1])
  return saturations

def getValuesV3(categorey):
  values = []
  for i in range(len(categorey)):
    values.append([])
  for i in range(len(categorey)):
    for j in range(len(categorey[i])):
      values[i].append(categorey[i][j][2])
  return values

#Getting the maximum and minimum of each h,s,v of each category
def setBoundaries(hues, saturations, values):

  hueMin = []
  hueMax = []
  satMin = []
  satMax = []
  valMin = []
  valMax = []

  for i in range(len(hues)):
    hueMin.append(min(hues[i]))
    hueMax.append(max(hues[i]))

  for i in range(len(saturations)):
    satMin.append(min(saturations[i]))
    satMax.append(max(saturations[i]))

  for i in range(len(values)):
    valMin.append(min(values[i]))
    valMax.append(max(values[i]))
  return hueMin, hueMax, satMin, satMax, valMin, valMax

"""# Colour Segmentation"""

#Producing List of images, each image contains a shape of different Colour (imgResult)
def segment(hueMin, hueMax, satMin, satMax, valMin, valMax):
  lowerRange = []
  upperRange = []
  for i in range(len(hueMin)):
    #lowerRange.append([hueMin[i], satMin[i]  , valMin[i]])
    lowerRange.append([hueMin[i], np.uint8(satMin[i] +15)  , valMin[i]])
    upperRange.append([hueMax[i], satMax[i], valMax[i]])
  lower_range = np.array(lowerRange)
  upper_range = np.array(upperRange)
  return lower_range, upper_range


def masking(lower_range, upper_range, hsvMedian, img):
  mask = []
  imgResult = []
  for i in range(len(lower_range)):
    mask.append(cv2.inRange(hsvMedian, lower_range[i], upper_range[i]))
  for j in range(len(lower_range)):
    imgResult.append(cv2.bitwise_and(img, img, mask=mask[j]))
  return imgResult

"""# Preprocessing"""

# Returning a Clean Image ready to be used in measurement
def bluring(imgResult):
  imgGray = []
  imgBlur = []
  for i in range(len(imgResult)):
    imgGray.append(cv2.cvtColor(imgResult[i], cv2.COLOR_BGR2GRAY))
  for i in range(len(imgGray)):
    imgBlur.append(cv2.GaussianBlur(imgGray[i], (5,5),3))
  return imgBlur

def preprocessing(imgBlur):
  imTT = []
  th2 = []
  for i in range(len(imgBlur)):
    imTT.append([])
  for k in range(len(imgBlur)):
    for i in range(imgBlur[k].shape[1]):
      for j in range(imgBlur[k].shape[0]):
        if imgBlur[k][j][i] > 5:
          imTT[k].append(imgBlur[k][j][i])
  modd = []
  for i in range(len(imTT)):
    modd.append(mode(imTT[i]))
  for i in range(len(imgBlur)):
    th2.append(cv2.threshold(imgBlur[i],modd[i]-70,255,cv2.THRESH_BINARY))
  return th2

def is_contour_bad(c):
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, 0.02 * peri, True)
	return  cv2.contourArea(c) < 1200

def beforeCelaning(maskBad, th2):
  imgb4Cleaned = []
  for i in range(len(th2)):
    imgb4Cleaned.append(th2[i][1].copy())
  return imgb4Cleaned

def badMask(imgResult, img):
  maskBad = []
  for i in range(len(imgResult)):
    maskBad.append(np.ones(img.shape[:2], dtype="uint8") * 255)
  return maskBad

def applyBadContours(imgb4Cleaned, th2, maskBad):
  imgCleaned = []
  for j in range(len(th2)):
    contours, hierarchy = cv2.findContours(image=th2[j][1], mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)
    for k, cnt in enumerate(contours):
      if is_contour_bad(cnt):
        cv2.drawContours(maskBad[j], [cnt], -1, 0, -1)
    imgCleaned.append(cv2.bitwise_and(imgb4Cleaned[j], th2[j][1], mask=maskBad[j]))
  return imgCleaned
def morph(imgCleaned):
  dilations = []
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
  for i in range(len(imgCleaned)):
    dilations.append(cv2.dilate(imgCleaned[i], kernel, iterations=2))
  return dilations

"""# (optional) If we want to get those rectangle colours in producing Legends"""

def extractNewRgb(imgResult):
  im = cv2.cvtColor(imgResult, cv2.COLOR_BGR2RGB)
  im = Image.fromarray(im)
  colors_x = extcolors.extract_from_image(im,2,2)

  listofTuples = []
  for i in range(len(colors_x[0])):
    listofTuples.append(colors_x[0][i][0])
  rgb = []
  for i in range(len(listofTuples)):
    rgb.append([])
  for i in range(len(listofTuples)):
    for j in range(len(listofTuples[i])):
      rgb[i].append(listofTuples[i][j])
  return rgb

def intRgb(imgResult):
  list_keda = []
  for i in range(len(imgResult)):
    list_keda.append(extractNewRgb(imgResult[i]))
  newRgb = []
  for i in range(len(list_keda)):
    newRgb.append(list_keda[i][1])

  list_int = []
  for i in range(len(newRgb)):
    list_int.append([])

  for i in range(len(newRgb)):
    for j in range(len(newRgb[i])):
      list_int[i].append(int(newRgb[i][j]))
  return list_int

def rgb2hexa(list_int):
  x = []
  for i in range(len(list_int)):
    x.append(rgb2hex(list_int[i][0],list_int[i][1],list_int[i][2]))
  return x

# (optional) If we want to get those rectangle colours in producing Legends

def extractNewRgb(imgResult):
  im = cv2.cvtColor(imgResult, cv2.COLOR_BGR2RGB)
  im = Image.fromarray(im)
  colors_x = extcolors.extract_from_image(im,2,2)

  listofTuples = []
  for i in range(len(colors_x[0])):
    listofTuples.append(colors_x[0][i][0])
  rgb = []
  for i in range(len(listofTuples)):
    rgb.append([])
  for i in range(len(listofTuples)):
    for j in range(len(listofTuples[i])):
      rgb[i].append(listofTuples[i][j])
  return rgb

def intRgb(imgResult):
  list_keda = []
  for i in range(len(imgResult)):
    list_keda.append(extractNewRgb(imgResult[i]))
  newRgb = []
  for i in range(len(list_keda)):
    newRgb.append(list_keda[i][1])

  list_int = []
  for i in range(len(newRgb)):
    list_int.append([])

  for i in range(len(newRgb)):
    for j in range(len(newRgb[i])):
      list_int[i].append(int(newRgb[i][j]))
  return list_int

def rgb2hexa(list_int):
  x = []
  for i in range(len(list_int)):
    x.append(rgb2hex(list_int[i][0],list_int[i][1],list_int[i][2]))
  return x


"""# Measuring Area

"""

# Produce List of shape's area in PPIXELS
def getArea(dilation):
  contourzz, hierarchy = cv2.findContours(image=dilation, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  areaa = 0

  for i, cnt3 in enumerate(contourzz):
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    area3 = cv2.contourArea(cnt3)
    areaa = areaa+area3
  return areaa

def measure(dilations):
  areas = []
  for i in range(len(dilations)):
    areas.append(getArea(dilations[i]))
  return areas

#Producing Image with drawing Contours and putting the number of areas as text on the shape
def getDrawing(page,ratio,dilation, img ,areaRatio,perimRatio):

  contourzz, hierarchy = cv2.findContours(image=dilation, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  areaa = 0
  perimeter = 0
  imgNew = img

  for i, cnt3 in enumerate(contourzz):
    shape=[]
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    area3 = cv2.contourArea(cnt3)
    areaRatio1= area3*areaRatio
    areaa = areaa+area3
    perimeter3 = cv2.arcLength(cnt3, True)
    perimeterRatio1=perimeter3*perimRatio
    perimeterr = perimeter+perimeter3
    approx = cv2.approxPolyDP(cnt3, 0.005 * perimeter3, True)
    for point in approx:
      x1, y1 = point[0] 
      shape.append([int(x1*ratio),int(y1*ratio)])
    #areaa = round(areaa * 15.25/13480.0, 3)


    cv2.putText(imgNew, f'Area :{areaa}', (x2, y2), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
    imgNew = cv2.drawContours(img, [cnt3], -1, (0,255,255), 3)
    annot = page.add_polygon_annot( points=shape )  # 'Polygon'
    annot.set_border(width=0.3, dashes=[2])
    annot.set_colors( fill=(  0 ,  1, 1 ) ) 
    # annot.set_colors( fill=(1,0,1) )
    annot.set_opacity(0.5)
    annot.set_info(content='Area='+str(areaRatio1)+' m2' +'\n \nPerimeter='+str(perimeterRatio1)+' m',subject='ADR Team')#,title='uuum')
    # annot.set_line_ends(fitz.PDF_ANNOT_LE_DIAMOND, fitz.PDF_ANNOT_LE_CIRCLE)
    annot.update()
  # doc.save('tameem.pdf', deflate=True)
  return imgNew


def exDraw(plan, dilations, img, pdfpath,areaRatio,perimRatio):
  ## 
  doc = fitz.open('dropbox_plans/3.2/'+plan)
  page = doc[0]    
  page.set_rotation(0)
  pix=page.get_pixmap()
  ratio =  pix.width/ img.shape[1] 
  ###
  print(pdfpath)
  newImg = img
  for i in range(len(dilations)):
    newImg = (getDrawing(page,ratio, dilations[i], img ,areaRatio,perimRatio))
  # doc.save('tameem.pdf', deflate=True)
  pdflink= db.dropbox_upload_file(doc=doc,pdfname=plan,pdfpath=pdfpath)
  
  print(pdflink)
  return newImg , pdflink
# Perimeter

def getPerimeter(dilation):
  contourzz, hierarchy = cv2.findContours(image=dilation, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  perimeter = 0
  for i, cnt3 in enumerate(contourzz):
    #imgResult4 = img.copy()
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    perimeter3 = cv2.arcLength(cnt3, True)
    perimeter = perimeter+perimeter3
  return perimeter
def measureP(dilations):
  perimeters = []
  for i in range(len(dilations)):
    perimeters.append(getPerimeter(dilations[i]))
  return perimeters

# Number of Shapes


def getNumber(dilation):
  contourzz, hierarchy = cv2.findContours(image=dilation, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  n_shapes = 0
  for i, cnt3 in enumerate(contourzz):
    #imgResult4 = img.copy()
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    n_shapes = n_shapes + 1
  return n_shapes

def countShapes(dilations):
  n_shapes = []
  for i in range(len(dilations)):
    n_shapes.append(getNumber(dilations[i]))
  return n_shapes

# Length


def getLength(dilation):
  contourzz, hierarchy = cv2.findContours(image=dilation, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  perimeter = 0
  for i, cnt3 in enumerate(contourzz):
    #imgResult4 = img.copy()
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    x, y, w, h = cv2.boundingRect(cnt3)
  return w
def measureL(dilations):
  Length = []
  for i in range(len(dilations)):
    Length.append(getLength(dilations[i]))
  return Length

"""# Converting Pixels to M2

"""

#Producing Reference Object and calculating it's vale in PIXELS
def calcRef(img):
  blk = np.ones(img.shape, dtype="uint8") * [[[np.uint8(0), np.uint8(0), np.uint8(0)]]]

  start_point = (50, 100)
  end_point = (120, 200)
  color = (255, 255, 255) # white BGR
  thickness = -1 # Thickness of -1 will fill the entire shape

  blk = cv2.rectangle(blk, start_point, end_point, color, thickness)

  blk = cv2.cvtColor(blk, cv2.COLOR_BGR2GRAY)

  contourzz, hierarchy = cv2.findContours(image=blk, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE)
  for i, cnt3 in enumerate(contourzz):
    M = cv2.moments(cnt3)
    if M['m00'] != 0.0:
      x2 = int(M['m10']/M['m00'])
      y2 = int(M['m01']/M['m00'])
    area = cv2.contourArea(cnt3)
    return area

# Saving the Area of the Reference object in M2 that we get from the user
def getRealRef(area):
  areaFromUser = area
  return areaFromUser

# Calculating the M2 areas of each shape and saving them in a List
# areaGotFromUser --> User will pass it after measuring the produced shape in BB
# areaPixelRef --> will get it from calcRef() function
# areas --> will get it from the measure() function in the "Measuring Area Cells"
def m2R(PixelMetricRatio, areas):
  mreal = []
  for i in range(len(areas)):
    mreal.append(areas[i] * PixelMetricRatio) 
  return mreal

"""# Another approach (Getting the display screen dimensions from user)"""

def ppi_calculate(width, height, inch):
  diagonal = math.sqrt((width**2 )+(height**2))
  ppi = diagonal / inch
  ppi2 = ppi * ppi
  return ppi2
def convertToMeterSQ(ratio, areas):
  areaM2 = []
  for i in range(len(areas)):
    areaM2.append(round(areas[i] * ratio, 3)) # true value of area of any shape/ area px value of same shape
  return areaM2

"""# Main Function That calls all of the above functions"""

def mainFunction(plan, areaRatio ,perimRatio, pdfpath):
    plan1='dropbox_plans/3.2/'+str(plan)
    img = convert2img(plan1)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    imgChange = changeGrayModify(img)
    hsv = readImgggg(imgChange)
    clr_hsv = extractClrs(imgChange)
    clr_hsv_short = deleteZeroHue(clr_hsv)
    hInput, sInput, vInput = dividingInput(clr_hsv_short)
    lower, upper = setRange(5, hInput)
    cat2 = categorizeV4(hsv, lower, upper, img)
    hues = getHuesV3(cat2)
    values = getValuesV3(cat2)
    saturations = getSaturationsV3(cat2)
    hueMin, hueMax, satMin, satMax, valMin, valMax = setBoundaries(hues, saturations, values)
    lower_range, upper_range = segment(hueMin, hueMax, satMin, satMax, valMin, valMax)
    imgResult = masking(lower_range, upper_range, hsv, img)
    ##For the Legend newRgb and hexClrs, make the main function returns one of the following two variables
    ## newRgb or hexClrs
    newRgb = intRgb(imgResult)
    hexClrs = rgb2hexa(newRgb)
    imgBlur = bluring(imgResult)
    th2 = preprocessing(imgBlur)
    maskBad = badMask(imgResult, img)
    imgb4Cleaned = beforeCelaning(maskBad,th2)
    imgCleaned = applyBadContours(imgb4Cleaned, th2, maskBad)
    dilations = morph(imgCleaned)
    # newImg , pdflink = exDraw(plan, dilations, img , pdfpath)
    areas = measure(imgCleaned)
    perimeters = measureP(imgCleaned)
    quantites = countShapes(imgCleaned)
    length = measureL(imgCleaned)

    # area_ref_pixel, perimeter_ref_pixel = calcRef(img)

    # area_ref_real = getRealRef(areaRef)
    # perimeter_ref_real = getRealRefL(perimeterRef)
    realArea = m2R(areaRatio, areas)
    realPerimeter = m2R(perimRatio, perimeters)

    newImg  , pdflink= exDraw(plan,dilations, img, pdfpath,areaRatio, perimRatio )
    data = {
        'Color' : newRgb,
        'Occurences': quantites,
        'Area':realArea,
        'Total Area': realArea,
        'Perimeter':perimeters,
        'Total Perimeter': perimeters,
        'Length': length,
        'Total Length': length}
    df = pd.DataFrame(data)
    print(df)


    gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr= pilecaps_adr.legendGoogleSheets(df,plan ,pdfpath)

    dbx=db.dropbox_connect()

    md, res =dbx.files_download(path= pdfpath+plan)
    data = res.content

    doc=fitz.open("pdf", data)
    # list1=pd.DataFrame(columns=['content', 'creationDate', 'id', 'modDate', 'name', 'subject', 'title'])
    list1=pd.DataFrame(columns=['content',  'id',  'subject'])
    for page in doc:
        for annot in page.annots():
            list1.loc[len(list1)] =annot.info
    print(list1)
    return newImg, df , pdflink , spreadsheetId, spreadsheet_url   , list1