forked from soumikmohianuta/pixtoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshotProcessor.py
More file actions
161 lines (128 loc) · 5.54 KB
/
screenshotProcessor.py
File metadata and controls
161 lines (128 loc) · 5.54 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 4 00:16:19 2017
@author: soumi
"""
#import numpy as np
import cv2
from viewProcessor.Canny import Canny
from viewProcessor.ContourAnalysis import ContourAnalysis
from viewProcessor.ContourAnalysis import ContourInfo
from Utils.Project import Project
from viewProcessor.HierarchyInfo import ViewHierarchyProcessor
from Utils.DipCalculator import DipCalculator
from Utils.Resolution import Resolution
from Utils.Profile import Profile
from Utils import Environment
from ocr.TesseractOCR import TesseractOCR
from ocr.TextProcessor import TextProcessor
from Utils.ColorUtil import CColor
from ocr.TextInfo import TextInfo
from Utils import Util
from layout.RootAlignmentLayoutFilter import RootAlignmentLayoutFilter
from layout.RelativeLayoutFilter import RelativeLayoutFilter
from layout.DefaultLayoutCreator import DefaultLayoutCreator
from layout.LayoutCreatorForList import LayoutCreatorForList
from layout.LayoutCreator import LayoutCreator
import layout.LayoutHelper as LayoutHelper
from layout.LayoutFilter import LayoutFilter
from projectUtil import ProjectGenerator
import copy
from Utils import XmlUtil
from Utils import Constants
from resource.DrawableWriter import DrawableWriter
import os, sys
from projectUtil.ProjectInfo import ProjectInfo
PROJECT_FOLDER = ""
def generateProjectName(mFileName):
filename, file_extension = os.path.splitext(mFileName)
mProjectName = Util.getProjectName(filename)
return mProjectName
def generateProject(imageLocation):
fileExitst = os.path.isfile(imageLocation)
if(not fileExitst):
return "Can't access the file"
img_color = cv2.imread(imageLocation)
img_gray = copy.deepcopy(img_color)
if (len(img_color.shape)==3):
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
profile = Profile(Resolution.XXHDPI, img_gray.shape[1], img_gray.shape[0])
dipCalculator = DipCalculator(img_color,profile)
width = 0
height = 0
if len(img_color.shape) == 2 :
height, width = img_color.shape
else:
height, width,channels = img_color.shape
#create a valid project name and package name
mProjectName = generateProjectName(imageLocation)
mOutProjectFolder = PROJECT_FOLDER + mProjectName
#create project info
mProjectInfo = ProjectInfo(mProjectName, mOutProjectFolder)
filename, file_extension = os.path.splitext(imageLocation)
mDrawableWriter = DrawableWriter(file_extension, mOutProjectFolder)
#generate project from project template
ProjectGenerator.setup(mProjectInfo)
# dilate and find edges in the provided screenshot
dst_denoised = cv2.fastNlMeansDenoising(img_gray)
canny = Canny()
dst_edge = canny.findEdge(img_gray)
# project = Project("sample")
dst_edge_dilate = canny.addDilate(dst_edge)
contourAnalysis = ContourAnalysis()
contours = contourAnalysis.findContoursWithCanny(dst_edge_dilate)
contoursOutput = contourAnalysis.analyze(dst_edge_dilate, contours)
#do the hierarchy processing
hierarchyProcessor = ViewHierarchyProcessor(contoursOutput.rootView, img_color, canny)
hierarchyInfo = hierarchyProcessor.process()
# use tesseract to detect the text
tesseractOCR = TesseractOCR(dst_denoised,dipCalculator,"English")
textProcessor = TextProcessor(img_color,dst_denoised, hierarchyInfo.biMapViewRect, tesseractOCR, dipCalculator)
# process text to remove invalid texts
textInfo = textProcessor.processText(CColor.Red)
# Add text boxes to hierarchy
hierarchyProcessor.addTextToHierarchy(textInfo)
#print(contoursOutput.rootView)
#print(mProjectName)
#print(tesseractOCR)
#print(mDrawableWriter)
#print(img_color)
#print(mOutProjectFolder)
#print(dipCalculator)
print('cc')
# creator = LayoutCreatorForList(contoursOutput.rootView, mProjectName, tesseractOCR, mDrawableWriter, img_color, mFileName,
# mOutLogFolder, mOutProjectFolder, dipCalculator)
# else:
creator = DefaultLayoutCreator(contoursOutput.rootView, mProjectName, tesseractOCR, mDrawableWriter, img_color, mOutProjectFolder, dipCalculator)
# create layout
layoutDocument = creator.createDocument()
layoutFilter = LayoutFilter()
anotateMap = layoutFilter.anotate(layoutDocument)
layoutFilter = RelativeLayoutFilter()
layoutFilter.doFilter(layoutDocument, anotateMap)
layoutFilter = RootAlignmentLayoutFilter()
layoutFilter.doFilter(layoutDocument, anotateMap)
# write to xml
XmlUtil.writeDocumentxml(layoutDocument, mOutProjectFolder + Constants.DEFAULT_LAYOUT_PATH + "activity_main.xml")
# write style
styleWriter = creator.mStyleWriter
styleDocument = styleWriter.mRoot
XmlUtil.writeDocumentxml(styleDocument, mOutProjectFolder + "/app/src/main/res/values/styles.xml")
#write to color file
colorWriter = creator.mColorWriter
colorDocument = colorWriter.mRoot
XmlUtil.writeDocumentxml(colorDocument, mOutProjectFolder + "/app/src/main/res/values/colors.xml")
# write to string file
stringWriter = creator.mWriter
resourceDocument = stringWriter.mRoot
XmlUtil.writeDocumentxml(resourceDocument, mOutProjectFolder + "/app/src/main/res/values/strings.xml")
# save drawable files
mDrawableWriter.save()
# compile and create a zip of the project
# ProjectGenerator.prepareProject(mProjectInfo)
# ProjectGenerator.unInstallAPK(mOutProjectFolder,mPackageName)
# ProjectGenerator.installAPK(mOutProjectFolder,mProjectName)
return
if __name__ =="__main__":
filename = sys.argv[1]
generateProject(filename)