Best JavaScript code snippet using wpt
polygonAnnotation.ts
Source:polygonAnnotation.ts
1import { GetBBox, GetBBoxIOU, IsInnerBBox, IsOuterBBox, MergeBBox, Point } from '@/api/utils/bbox'2import { Annotation, PolygonAnnotation, RingType } from '@/api/annotation'3// å¡«å
ä¸è§ååºå4function FillArea (ctx: CanvasRenderingContext2D, points: Array<Point>): void {5 if (points.length <= 0) return6 const pStart = points[0]7 ctx.beginPath()8 ctx.moveTo(pStart[0], pStart[1])9 for (let i = 1; i < points.length; i++) {10 const p = points[i]11 ctx.lineTo(p[0], p[1])12 }13 ctx.fill()14 ctx.closePath()15}16// å¤æ第ä¸ä¸ªæ 注æ¯å¦è¢«ç¬¬äºä¸ªæ 注å
å«17export function IsInnerAnnotation (18 annotation1: PolygonAnnotation,19 annotation2: PolygonAnnotation,20 bboxWindow: number[],21 ctx: CanvasRenderingContext2D): boolean {22 if (!ctx) return false23 ctx.clearRect(0, 0, bboxWindow[2], bboxWindow[3])24 ctx.lineWidth = 125 ctx.fillStyle = `rgba(${annotation2.id},${0},${0}, 1)`26 FillArea(ctx, annotation2.coordinates[0])27 const bbox = annotation1.bbox28 const width = bbox[2] - bbox[0] + 129 const height = bbox[3] - bbox[1] + 130 const bboxArea = ctx.getImageData(bbox[0], bbox[1], width, height)31 let count = 032 annotation1.coordinates[0].forEach(p => {33 const t = Math.floor(p[1] - bbox[1]) * width + Math.floor(p[0] - bbox[0])34 if (bboxArea.data[4 * t] > 0) {35 count += 136 }37 })38 return (count / annotation1.coordinates[0].length) > 0.9739}40// 计ç®ä¸¤ä¸ªæ 注çéå 度 iou41export function GetAnnotationIOU (annotation1: PolygonAnnotation, annotation2: PolygonAnnotation, bboxWindow: number[], ctx: CanvasRenderingContext2D): number {42 let iou = 043 if (!ctx) return iou44 const bbox = MergeBBox(annotation1.bbox, annotation2.bbox)45 const width = bbox[2] - bbox[0] + 146 const height = bbox[3] - bbox[1] + 147 let count1 = 048 let count2 = 049 ctx.clearRect(0, 0, bboxWindow[2], bboxWindow[3])50 ctx.lineWidth = 151 ctx.fillStyle = `rgba(${1},${0},${0}, 0.5)`52 FillArea(ctx, annotation1.coordinates[0])53 ctx.fillStyle = `rgba(${2},${0},${0}, 0.5)`54 FillArea(ctx, annotation2.coordinates[0])55 const bboxArea = ctx.getImageData(bbox[0], bbox[1], width, height)56 for (let t = 0; t < bboxArea.data.length; t += 4) {57 const value = bboxArea.data[t]58 if (value === 1) {59 count1 += 160 } else if (value === 2) {61 count2 += 162 }63 }64 iou = count1 / (count1 + count2)65 console.log('iou', iou, count1, count2)66 return iou67}68// å¤æ第ä¸ä¸ªæ 注æ¯å¦æ¯ç¬¬äºä¸ªæ 注çç¶èç¹69function IsOuterRing (70 annotation1: PolygonAnnotation,71 annotation2: PolygonAnnotation,72 bbox: number[],73 ctx: CanvasRenderingContext2D): boolean {74 if (IsOuterBBox(annotation1.bbox, annotation2.bbox)) {75 if (IsInnerAnnotation(annotation2, annotation1, bbox, ctx)) {76 return true77 }78 }79 return false80}81// æ¾å°ç¶èç¹82function FindOuterRing (83 annotations: Annotation[],84 annotation1: PolygonAnnotation,85 annotation2: PolygonAnnotation,86 bbox: number[],87 ctx: CanvasRenderingContext2D): number {88 if (IsInnerBBox(annotation1.bbox, annotation2.bbox)) {89 if (annotation2.innerRingIDs.size === 0) {90 if (IsInnerAnnotation(annotation1, annotation2, bbox, ctx)) {91 return annotation2.id92 }93 return -194 }95 let flag = false96 let idx = -197 for (const id of annotation2.innerRingIDs) {98 const annotationT = annotations.find((item) => item.id === id) as PolygonAnnotation99 idx = FindOuterRing(annotations, annotation1, annotationT, bbox, ctx)100 if (idx !== -1) {101 flag = true102 break103 }104 }105 if (flag) return idx106 else {107 if (IsInnerAnnotation(annotation1, annotation2, bbox, ctx)) {108 return annotation2.id109 } else return -1110 }111 }112 return -1113}114// æ 注çç¯ç±»åæ´æ°115function UpdateAnnotationRingType (annotations: Annotation[], annotation: PolygonAnnotation, ringType: RingType): void {116 if (annotation.ringType === ringType) return117 annotation.ringType = ringType118 if (annotation.innerRingIDs.size === 0 && annotation.outerRingId === -1) {119 annotation.ringType = RingType.NORMAL120 return121 }122 for (const id of annotation.innerRingIDs) {123 const annotationT = annotations.find((item) => item.id === id) as PolygonAnnotation124 if (annotationT.tag === annotation.tag) {125 if (ringType === RingType.CUT) {126 UpdateAnnotationRingType(annotations, annotationT, RingType.INNER)127 } else if (ringType === RingType.INNER) {128 UpdateAnnotationRingType(annotations, annotationT, RingType.CUT)129 } else if (ringType === RingType.OUTER) {130 UpdateAnnotationRingType(annotations, annotationT, RingType.CUT)131 }132 }133 }134}135// 设置æ 注çç¯ç±»å136export function SetAnnotationRingType (137 annotations: Annotation[],138 annotation: PolygonAnnotation,139 bbox: number[],140 ctx: CanvasRenderingContext2D): void {141 if (annotation.ringType !== RingType.NONE) return142 // ä½ä¸ºåèç¹æ¾å°ç¶äº²143 let outerId = -1144 const innerIds = []145 const innerIndices = []146 for (let i = 0; i < annotations.length; i++) {147 if (annotations[i].type !== 'Polygon' || annotations[i].id === annotation.id) continue148 const annotationT = annotations[i] as PolygonAnnotation149 if (annotationT.ringType === RingType.NORMAL || annotationT.ringType === RingType.OUTER) {150 outerId = FindOuterRing(annotations, annotation, annotationT, bbox, ctx)151 if (outerId !== -1) {152 break153 } else {154 if (IsOuterRing(annotation, annotationT, bbox, ctx)) {155 innerIds.push(annotationT.id)156 innerIndices.push(i)157 }158 }159 }160 }161 if (outerId !== -1) {162 const annotationOuter = annotations.find((item) => item.id === outerId) as PolygonAnnotation163 if (annotationOuter.innerRingIDs.size === 0) { // ä»
ä½ä¸ºå
ç¯164 annotationOuter.innerRingIDs.add(annotation.id)165 annotation.outerRingId = annotationOuter.id166 let ringType = RingType.INNER167 if (annotation.tag === annotationOuter.tag && annotationOuter.ringType !== RingType.CUT) {168 ringType = RingType.CUT169 }170 annotation.ringType = ringType171 if (annotationOuter.ringType === RingType.NORMAL) {172 annotationOuter.ringType = RingType.OUTER173 }174 } else { // èèæ¢ä½ä¸ºå
ç¯åä½ä¸ºå¤ç¯175 annotationOuter.innerRingIDs.add(annotation.id)176 annotation.outerRingId = annotationOuter.id177 annotation.ringType = RingType.INNER178 if (annotationOuter.tag === annotation.tag && annotationOuter.ringType !== RingType.CUT) {179 annotation.ringType = RingType.CUT180 }181 for (const id of annotationOuter.innerRingIDs) {182 const index = annotations.findIndex((item) => item.id === id)183 const annotationT = annotations[index] as PolygonAnnotation184 if (IsOuterRing(annotation, annotationT, bbox, ctx)) {185 annotationT.outerRingId = annotation.id186 annotation.innerRingIDs.add(annotationT.id)187 annotationOuter.innerRingIDs.delete(annotationT.id)188 let ringType = RingType.INNER189 if (annotation.tag === annotationT.tag && annotation.ringType !== RingType.CUT) {190 ringType = RingType.CUT191 }192 UpdateAnnotationRingType(annotations, annotationT, ringType)193 }194 }195 }196 } else if (innerIndices.length > 0) { // ä»
ä½ä¸ºå¤ç¯197 annotation.ringType = RingType.OUTER198 for (let i = 0; i < innerIndices.length; i++) {199 const annotationT = annotations[innerIndices[i]] as PolygonAnnotation200 annotation.innerRingIDs.add(annotationT.id)201 annotationT.outerRingId = annotation.id202 if (annotation.tag === annotationT.tag) {203 UpdateAnnotationRingType(annotations, annotationT, RingType.CUT)204 } else {205 UpdateAnnotationRingType(annotations, annotationT, RingType.INNER)206 }207 }208 } else { // ä»
ä½ä¸ºæ®éç¯209 annotation.ringType = RingType.NORMAL210 }211}212function GetMergeRectArea (annotations1: Annotation[], annotations2: PolygonAnnotation[]): number[] {213 let xMax = -1214 let yMax = -1215 annotations1.forEach(annotation => {216 if (annotation.type === 'Polygon') {217 annotation = annotation as PolygonAnnotation218 if (annotation.ringType === RingType.NORMAL || annotation.ringType === RingType.OUTER) {219 xMax = Math.max(xMax, annotation.bbox[2])220 yMax = Math.max(yMax, annotation.bbox[3])221 }222 }223 })224 annotations2.forEach(annotation => {225 xMax = Math.max(xMax, annotation.bbox[2])226 yMax = Math.max(yMax, annotation.bbox[3])227 })228 xMax += 20229 yMax += 20230 return [0, 0, xMax, yMax]231}232function GetMergeCtx (bbox: number[]): CanvasRenderingContext2D {233 const canvas0 = document.querySelector('.MergeLevelSet') as HTMLCanvasElement234 if (canvas0) {235 document.body.removeChild(canvas0)236 }237 const canvas = document.createElement('canvas') as HTMLCanvasElement238 canvas.width = bbox[2]239 canvas.height = bbox[3]240 canvas.style.display = 'none'241 canvas.className = '.MergeLevelSet'242 return canvas.getContext('2d') as CanvasRenderingContext2D243}244// å° level Setæ 注 å并å°æ®éæ 注ä¸245export function MergeLevelSetAnnotations (246 annotations: Annotation[],247 levelSetAnnotations: PolygonAnnotation[]): Annotation[] {248 const bbox = GetMergeRectArea(annotations, levelSetAnnotations)249 const ctx = GetMergeCtx(bbox)250 const polygonAnnotations: PolygonAnnotation[] = []251 let maxId = 0252 annotations.forEach(annotation => {253 if (annotation.type === 'Polygon') {254 polygonAnnotations.push(annotation as PolygonAnnotation)255 }256 if (annotation.id > maxId) maxId = annotation.id257 })258 levelSetAnnotations = levelSetAnnotations.filter(annotationL => {259 for (let i = 0; i < polygonAnnotations.length; i++) {260 if (GetBBoxIOU(polygonAnnotations[i].bbox, annotationL.bbox) > 0.1 &&261 GetAnnotationIOU(polygonAnnotations[i], annotationL, bbox, ctx) > 0.60) {262 return false263 }264 }265 return true266 })267 levelSetAnnotations.forEach(annotationL => {268 maxId += 1269 annotationL.id = maxId270 annotations.push(annotationL)271 SetAnnotationRingType(annotations, annotationL, bbox, ctx)272 })273 return annotations274}275function FindMaxIdInAnnotations (annotations: Annotation[]): number {276 let maxId = -1277 annotations.forEach(annotation => {278 if (annotation.id > maxId) maxId = annotation.id279 })280 return maxId281}282// æ ¹æ®graph cut åæ çæä¸ä¸ª graph cutæ 注283export function CreateGraphCutAnnotation (labelTag: string, coordinates: Array<Array<[number, number]>>, annotations: Annotation[]): PolygonAnnotation {284 const annotation: PolygonAnnotation = {} as PolygonAnnotation285 annotation.type = 'Polygon'286 const maxId = FindMaxIdInAnnotations(annotations)287 annotation.id = maxId === -1 ? 1 : maxId + 1288 annotation.tag = labelTag289 annotation.coordinates = []290 annotation.coordinates.push([...coordinates[0]])291 annotation.bbox = GetBBox(annotation.coordinates[0])292 annotation.lines = []293 annotation.ringType = RingType.NONE294 annotation.innerRingIDs = new Set<number>()295 annotation.outerRingId = -1296 const idx = annotations.findIndex(item => item.tag === labelTag)297 annotation.isFill = idx !== -1 ? annotations[idx].isFill : false298 annotation.isShow = true299 annotation.isLevelSet = false300 return annotation301}302// å° graph cutæ 注 å并å°æ®éæ 注ä¸303export function MergeGraphCutAnnotations (304 graphCutAnnotation: PolygonAnnotation,305 annotations: Annotation[]): Annotation[] {306 const bbox = GetMergeRectArea(annotations, [graphCutAnnotation])307 const ctx = GetMergeCtx(bbox)308 annotations.push(graphCutAnnotation)309 SetAnnotationRingType(annotations, graphCutAnnotation, bbox, ctx)310 return annotations311}312// PolygonAnnotationç深度æ·è´313export function DeepCopyPolygonAnnotation (annotation: PolygonAnnotation): PolygonAnnotation {314 const annotationNew = { ...annotation }315 annotationNew.coordinates = annotation.coordinates.map(item => [...item])316 annotationNew.bbox = [...annotation.bbox]317 annotationNew.innerRingIDs = new Set([...annotation.innerRingIDs])318 annotationNew.lines = annotation.lines.map(item => [...item])319 return annotationNew...
polygon-annotation.ts
Source:polygon-annotation.ts
...31 if (dto.strokeDashGap) {32 bs.D = dto.strokeDashGap;33 }34 35 const annotation = new PolygonAnnotation();36 annotation.$name = dto.uuid;37 annotation.NM = LiteralString.fromString(dto.uuid);38 annotation.T = LiteralString.fromString(dto.author);39 annotation.M = DateString.fromDate(new Date(dto.dateModified));40 annotation.CreationDate = DateString.fromDate(new Date(dto.dateCreated));41 annotation.Contents = dto.textContent 42 ? LiteralString.fromString(dto.textContent) 43 : null;44 45 annotation.Rect = dto.rect;46 annotation.C = dto.color.slice(0, 3);47 annotation.CA = dto.color[3];48 annotation.BS = bs;49 annotation.IT = dto.cloud50 ? polyIntents.CLOUD51 : polyIntents.POLYGON_DIMENSION;52 annotation.Vertices = dto.vertices;53 annotation.generateApStream();54 annotation._added = true;55 return annotation.initProxy();56 }57 58 static async parseAsync(parseInfo: ParserInfo): Promise<ParserResult<PolygonAnnotation>> { 59 if (!parseInfo) {60 throw new Error("Parsing information not passed");61 } 62 try {63 const pdfObject = new PolygonAnnotation();64 await pdfObject.parsePropsAsync(parseInfo);65 return {66 value: pdfObject.initProxy(), 67 start: parseInfo.bounds.start, 68 end: parseInfo.bounds.end,69 };70 } catch (e) {71 console.log(e.message);72 return null;73 }74 } 75 76 override toArray(cryptInfo?: CryptInfo): Uint8Array {77 const superBytes = super.toArray(cryptInfo); ...
edit-polygon.component.ts
Source:edit-polygon.component.ts
...11 styleUrls: ['./edit-polygon.component.css']12})13export class EditPolygonComponent implements OnInit {14 @ViewChild(ToolDialogComponent, {static :false}) formatDialog: ToolDialogComponent15 @Input() item : PolygonAnnotation | PolylineAnnotation = new PolygonAnnotation()16 constructor(private tabs : ToolTabsComponent, private data : DataService, private session : LivePageComponent) { 17 }18 ngOnInit() {19 }20 showFormatDialog() {21 const dialog : FormatToolDialogComponent = this.tabs.showDialog(FormatToolDialogComponent)22 dialog.item = this.item23 dialog.onUpdate.subscribe( () => {24 this.session.limitedUpdates$.next(this.item)25 })26 }27 28 updated(event ?: any) {29 this.session.layerMgr.storeAnnotation(this.item)...
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var page = wptools.page('Barack Obama');8page.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var page = wptools.page('Barack Obama');13page.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var page = wptools.page('Barack Obama');18page.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var page = wptools.page('Barack Obama');23page.get(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var page = wptools.page('Barack Obama');28page.get(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var page = wptools.page('Barack Obama');33page.get(function(err, resp) {34 console.log(resp);35});36var wptools = require('wptools');37var page = wptools.page('Barack Obama');38page.get(function(err, resp) {39 console.log(resp);40});41var wptools = require('wptools');42var page = wptools.page('Barack Obama');43page.get(function(err, resp) {44 console.log(resp);45});
Using AI Code Generation
1var wptools = require('wptools');2var poly = new wptools.PolygonAnnotation();3poly.addPoint(1.23, 4.56);4poly.addPoint(7.89, 10.11);5poly.addPoint(12.13, 14.15);6poly.addPoint(16.17, 18.19);7poly.addPoint(20.21, 22.23);8console.log(poly.toString());9var wptools = require('wptools');10var poly = new wptools.PolylineAnnotation();11poly.addPoint(1.23, 4.56);12poly.addPoint(7.89, 10.11);13poly.addPoint(12.13, 14.15);14poly.addPoint(16.17, 18.19);15poly.addPoint(20.21, 22.23);16console.log(poly.toString());17var wptools = require('wptools');18var text = new wptools.TextAnnotation();19text.setText('This is a test');20text.setPosition(1.23, 4.56);21text.setJustification('right');22text.setPointSize(12);23text.setBold();24text.setItalic();25text.setUnderline();26text.setStrikeThrough();27console.log(text.toString());28var wptools = require('wptools');29var wp = new wptools.Wptools();30wp.addPoint(1.23, 4.56);31wp.addPoint(7.89, 10.11);32wp.addPoint(12.13, 14.15);33wp.addPoint(16.17, 18.19);34wp.addPoint(20.21, 22.23);35wp.addPoint(24.25, 26.27);36wp.addPoint(28.29, 30.31);37wp.addPoint(32.33, 34.35);38wp.addPoint(36.37, 38.39);39wp.addPoint(40.41, 42.43);40wp.addPoint(44.45, 46.47);41wp.addPoint(48.49, 50
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Eiffel Tower');3wp.get_polygons(function(err, polygons) {4 console.log(polygons);5});6var wptools = require('wptools');7var wp = wptools.page('Eiffel Tower');8wp.get_polygons(function(err, polygons) {9 console.log(polygons);10});11var wptools = require('wptools');12var wp = wptools.page('Eiffel Tower');13wp.get_polygons(function(err, polygons) {14 console.log(polygons);15});16var wptools = require('wptools');17var wp = wptools.page('Eiffel Tower');18wp.get_polygons(function(err, polygons) {19 console.log(polygons);20});21var wptools = require('wptools');22var wp = wptools.page('Eiffel Tower');23wp.get_polygons(function(err, polygons) {24 console.log(polygons);25});26var wptools = require('wptools');27var wp = wptools.page('Eiffel Tower');28wp.get_polygons(function(err, polygons) {29 console.log(polygons);30});31var wptools = require('wptools');32var wp = wptools.page('Eiffel Tower');33wp.get_polygons(function(err, polygons) {34 console.log(polygons);35});36var wptools = require('wptools');37var wp = wptools.page('Eiffel Tower');38wp.get_polygons(function(err, polygons) {39 console.log(polygons);40});41var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4var options = {5};6var page = wptools.page('New York', options);7page.get_polygons(function(err, polygons) {8 if (err) {9 console.log(err);10 } else {11 console.log(polygons);12 }13});14var wptools = require('wptools');15var fs = require('fs');16var path = require('path');17var options = {18};19var page = wptools.page('New York', options);20page.get_polygons(function(err, polygons) {21 if (err) {22 console.log(err);23 } else {24 console.log(polygons);25 }26});27var wptools = require('wptools');28var fs = require('fs');29var path = require('path');30var options = {31};32var page = wptools.page('New York', options);33page.get_polygons(function(err, polygons) {34 if (err) {35 console.log(err);36 } else {37 console.log(polygons);38 }39});40var wptools = require('wptools');41var fs = require('fs');42var path = require('path');43var options = {44};45var page = wptools.page('New York', options);46page.get_polygons(function(err, polygons) {47 if (err) {48 console.log(err);49 } else {50 console.log(polygons);51 }52});53var wptools = require('wptools');54var fs = require('fs');55var path = require('path');56var options = {57};58var page = wptools.page('New York', options
Using AI Code Generation
1var wptools = require('wptools');2 {lat: 40.751, lon: -74.006},3 {lat: 40.753, lon: -74.006},4 {lat: 40.753, lon: -74.004},5 {lat: 40.751, lon: -74.004},6 {lat: 40.751, lon: -74.006}7];8var options = {9};10wptools.polygonAnnotation(options, function(err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});
Using AI Code Generation
1var wptools = require('wptools');2var poly = new wptools.PolygonAnnotation();3poly.addPoint(0, 0);4poly.addPoint(0, 1);5poly.addPoint(1, 1);6poly.addPoint(1, 0);7var ann = new wptools.Annotation();8ann.setType('Polygon');9ann.setPolygon(poly);10ann.setPage('Test');11ann.setAuthor('TestUser');12ann.setContents('Test Annotation');13ann.save(function(err, ann){14 if(err){15 console.log('Error: ' + err);16 } else {17 console.log('Annotation created: ' + ann);18 }19});20var wptools = require('wptools');21var poly = new wptools.PolygonAnnotation();22poly.addPoint(0, 0);23poly.addPoint(0, 1);24poly.addPoint(1, 1);25poly.addPoint(1, 0);26var ann = new wptools.Annotation();27ann.setType('Polygon');28ann.setPolygon(poly);29ann.setPage('Test');30ann.setAuthor('TestUser');31ann.setContents('Test Annotation');32ann.save(function(err, ann){33 if(err){34 console.log('Error: ' + err);35 } else {36 console.log('Annotation created: ' + ann);37 }38});39var wptools = require('wptools');40var poly = new wptools.PolygonAnnotation();41poly.addPoint(0, 0);42poly.addPoint(0, 1);43poly.addPoint(1, 1);44poly.addPoint(1, 0);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!