Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
Objekterkennung in AR mit ARKit und CoreML
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ufuk Kaan Tugay
Objekterkennung in AR mit ARKit und CoreML
Commits
288a9bf2
Commit
288a9bf2
authored
3 years ago
by
Ufuk Kaan Tugay
Browse files
Options
Downloads
Patches
Plain Diff
Replace ViewController.swift
parent
62e625f3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Objekterkennung in AR mit ARKit und CoreML/ViewController.swift
+189
-115
189 additions, 115 deletions
...erkennung in AR mit ARKit und CoreML/ViewController.swift
with
189 additions
and
115 deletions
Objekterkennung in AR mit ARKit und CoreML/ViewController.swift
+
189
−
115
View file @
288a9bf2
...
@@ -12,15 +12,61 @@ import Vision
...
@@ -12,15 +12,61 @@ import Vision
class
ViewController
:
UIViewController
,
ARSCNViewDelegate
{
class
ViewController
:
UIViewController
,
ARSCNViewDelegate
{
@IBOutlet
var
sceneView
:
ARSCNView
!
@IBOutlet
var
sceneView
:
ARSCNView
!
var
detectionMode
=
"click"
var
detectionMode
=
"click"
var
node_array
:
[
SCNNode
]
=
[]
var
scene_bounds
:
(
CGFloat
,
CGFloat
)
=
(
0
,
0
)
var
scene_bounds
:
(
CGFloat
,
CGFloat
)
=
(
0
,
0
)
var
object_detector
=
ObjectDetection
()
var
object_detector
=
ObjectDetection
()
var
ar_functions
=
View
()
var
ar_functions
=
View
()
let
label
=
UILabel
(
frame
:
CGRect
(
x
:
5
,
y
:
20
,
width
:
400
,
height
:
30
))
let
label
=
UILabel
(
frame
:
CGRect
(
x
:
5
,
y
:
20
,
width
:
400
,
height
:
30
))
var
start_position_array
:
[
simd_float4x4
]
=
[]
var
classification_object
=
""
var
bounding_box_object
=
SCNNode
()
var
text_object
=
SCNNode
()
var
text_node
:
SCNNode
{
return
text_object
}
var
bounding_box_node
:
SCNNode
{
return
bounding_box_object
}
var
test_node
:
SCNNode
{
switch
classification_object
{
case
"90T_Tyre"
:
return
ar_functions
.
getTire90T
()
case
"beam_0412-140"
:
return
ar_functions
.
getBeam0412_140
()
case
"beam_0412-92"
:
return
ar_functions
.
getBeam0412_092
()
case
"beam_0824-112"
:
return
ar_functions
.
getBeam0824_112
()
case
"beam_0824-16"
:
return
ar_functions
.
getBeam0824_016
()
case
"beam_0824-80"
:
return
ar_functions
.
getBeam0824_080
()
case
"beam_0824-96"
:
return
ar_functions
.
getBeam0824_096
()
case
"bracket_3x3"
:
return
ar_functions
.
getBracket3x3
()
case
"plate_3x6"
:
return
ar_functions
.
getPlate3x6
()
case
"unnamed_nut"
:
return
ar_functions
.
getBrassStud
()
case
"unnamed_angle"
:
return
ar_functions
.
getPlate45
()
default
:
let
no_model_node
=
SCNNode
()
no_model_node
.
name
=
"no_model"
return
no_model_node
}
}
var
start_position
:
simd_float4x4
{
return
sceneView
.
session
.
currentFrame
!.
camera
.
transform
}
func
createLabel
(){
func
createLabel
(){
if
detectionMode
==
"loop"
{
if
detectionMode
==
"loop"
{
...
@@ -33,26 +79,7 @@ class ViewController: UIViewController, ARSCNViewDelegate {
...
@@ -33,26 +79,7 @@ class ViewController: UIViewController, ARSCNViewDelegate {
self
.
view
.
addSubview
(
label
)
self
.
view
.
addSubview
(
label
)
}
}
func
createButtons
(){
let
mode_button
=
UIButton
()
mode_button
.
frame
=
CGRect
(
x
:
20
,
y
:
700
,
width
:
200
,
height
:
100
)
mode_button
.
backgroundColor
=
.
white
mode_button
.
setTitle
(
"Change Detection Mode"
,
for
:
.
normal
)
mode_button
.
setTitleColor
(
.
black
,
for
:
.
normal
)
mode_button
.
addTarget
(
self
,
action
:
#selector
(
changeDetectionMode
),
for
:
.
touchUpInside
)
let
detect_button
=
UIButton
()
detect_button
.
frame
=
CGRect
(
x
:
240
,
y
:
700
,
width
:
200
,
height
:
100
)
detect_button
.
backgroundColor
=
.
white
detect_button
.
setTitle
(
"Perform Detection"
,
for
:
.
normal
)
detect_button
.
setTitleColor
(
.
black
,
for
:
.
normal
)
detect_button
.
addTarget
(
self
,
action
:
#selector
(
detect
),
for
:
.
touchUpInside
)
self
.
view
.
addSubview
(
mode_button
)
self
.
view
.
addSubview
(
detect_button
)
}
@objc
func
changeDetectionMode
(){
@objc
func
changeDetectionMode
(){
if
detectionMode
==
"loop"
{
if
detectionMode
==
"loop"
{
...
@@ -73,13 +100,13 @@ class ViewController: UIViewController, ARSCNViewDelegate {
...
@@ -73,13 +100,13 @@ class ViewController: UIViewController, ARSCNViewDelegate {
}
}
override
func
viewDidLoad
()
{
override
func
viewDidLoad
()
{
super
.
viewDidLoad
()
super
.
viewDidLoad
()
let
configuration
=
ARWorldTrackingConfiguration
()
let
configuration
=
ARWorldTrackingConfiguration
()
configuration
.
planeDetection
=
.
horizontal
configuration
.
planeDetection
=
[
.
horizontal
,
.
vertical
]
sceneView
.
scene
=
SCNScene
()
sceneView
.
scene
=
SCNScene
()
sceneView
.
session
.
run
(
configuration
)
sceneView
.
session
.
run
(
configuration
)
...
@@ -90,122 +117,141 @@ class ViewController: UIViewController, ARSCNViewDelegate {
...
@@ -90,122 +117,141 @@ class ViewController: UIViewController, ARSCNViewDelegate {
createButtons
()
createButtons
()
createLabel
()
createLabel
()
}
}
override
func
viewWillAppear
(
_
animated
:
Bool
)
{
super
.
viewWillAppear
(
animated
)
}
override
func
viewWillDisappear
(
_
animated
:
Bool
)
{
super
.
viewWillDisappear
(
animated
)
}
func
renderer
(
_
renderer
:
SCNSceneRenderer
,
didAdd
node
:
SCNNode
,
for
anchor
:
ARAnchor
)
{
if
anchor
.
name
==
"model"
{
node
.
addChildNode
(
test_node
)
}
else
if
anchor
.
name
==
"bb"
{
bounding_box_node
.
position
=
node
.
position
node
.
addChildNode
(
bounding_box_node
)
}
else
if
anchor
.
name
==
"text"
{
text_node
.
position
=
node
.
position
node
.
addChildNode
(
text_node
)
}
}
func
performDetection
()
{
func
performDetection
()
{
guard
let
frame
=
sceneView
.
session
.
currentFrame
else
{
guard
let
frame
=
sceneView
.
session
.
currentFrame
else
{
return
return
}
}
if
frame
.
anchors
.
isEmpty
{
if
frame
.
anchors
.
isEmpty
{
print
(
"anchors are empty"
)
print
(
"anchors are empty"
)
return
return
}
}
let
start_time
=
Date
()
sceneView
.
scene
=
SCNScene
()
object_detector
.
detectObjects
(
on
:
sceneView
.
session
.
currentFrame
!.
capturedImage
)
object_detector
.
detectObjects
(
on
:
sceneView
.
session
.
currentFrame
!.
capturedImage
)
let
end_timee
=
Date
()
print
(
end_timee
.
timeIntervalSince
(
start_time
))
if
let
new_detection
=
self
.
object_detector
.
detection_object_array
.
first
{
if
new_detection
.
confidence
>
0.7
{
sceneView
.
scene
=
SCNScene
()
}
}
for
observations
in
self
.
object_detector
.
detection_object_array
{
for
observations
in
self
.
object_detector
.
detection_object_array
{
if
observations
.
confidence
>
0.5
{
if
observations
.
confidence
>
0.7
{
for
observations
in
self
.
object_detector
.
detection_object_array
{
for
observations
in
self
.
object_detector
.
detection_object_array
{
//print(observations)
//print(observations.boundingBox.midX)
//print(observations.boundingBox.midY)
DispatchQueue
.
main
.
async
{
DispatchQueue
.
main
.
async
{
self
.
scene_bounds
=
self
.
sceneBounds
()
self
.
scene_bounds
=
self
.
sceneBounds
()
}
}
classification_object
=
observations
.
classification
if
test_node
.
name
==
"no_model"
{
continue
}
var
translation
=
matrix_identity_float4x4
translation
.
columns
.
3
.
z
=
test_node
.
position
.
z
translation
.
columns
.
3
.
x
=
test_node
.
position
.
x
translation
.
columns
.
3
.
y
=
test_node
.
position
.
y
let
transform
=
simd_mul
(
start_position_array
[
0
],
translation
)
let
model_anchor
=
ARAnchor
(
name
:
"model"
,
transform
:
transform
)
sceneView
.
session
.
add
(
anchor
:
model_anchor
)
print
(
observations
.
classification
)
let
detectionLocation
=
VNImageRectForNormalizedRect
(
let
detectionLocation
=
VNImageRectForNormalizedRect
(
observations
.
boundingBox
,
observations
.
boundingBox
,
Int
(
self
.
scene_bounds
.
0
),
Int
(
self
.
scene_bounds
.
0
),
Int
(
self
.
scene_bounds
.
1
))
Int
(
self
.
scene_bounds
.
1
))
//self.objectDetector.detection_object_array.removeAll()
drawModel
(
observations
.
classification
)
//let bb2d_view = ar_functions.drawboundingbox2d(detectionLocation: detectionLocation)
//bb2d_view.removeFromSuperview()
//self.view.addSubview(bb2d_view)
ar_functions
.
drawBoundingBox3D
(
detectionLocation
:
detectionLocation
,
vision_bb
:
observations
.
boundingBox
,
sceneView
:
sceneView
)
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
draw_text_node
(
detectionLocation
:
detectionLocation
,
classification
:
observations
.
classification
,
sceneView
:
sceneView
))
let
end_time
=
Date
()
print
(
end_time
.
timeIntervalSince
(
start_time
))
}
// sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
let
bb_node
=
ar_functions
.
drawBoundingBox3D
(
detectionLocation
:
detectionLocation
,
sceneView
:
sceneView
)
// node.removeFromParentNode() }
bounding_box_object
=
bb_node
let
anchor_bb
=
ARAnchor
(
name
:
"bb"
,
transform
:
bb_node
.
simdTransform
)
sceneView
.
session
.
add
(
anchor
:
anchor_bb
)
let
drawn_text_node
=
ar_functions
.
draw_text_node
(
detectionLocation
:
detectionLocation
,
classification
:
observations
.
classification
,
sceneView
:
sceneView
)
text_object
=
drawn_text_node
let
anchor_text
=
ARAnchor
(
name
:
"text"
,
transform
:
drawn_text_node
.
simdTransform
)
sceneView
.
session
.
add
(
anchor
:
anchor_text
)
}
}
}
}
}
self
.
object_detector
.
detection_object_array
.
removeAll
()
self
.
object_detector
.
detection_object_array
.
removeAll
()
}
}
// func removeNodes(){
// self.sceneView.scene.rootNode.enumerateChildNodes { (existingNode, _) in
// node_array.append(existingNode)
// existingNode.removeFromParentNode()
// node_array.removeFirst()
// }
// }
func
drawModel
(
_
object_type
:
String
){
switch
object_type
{
case
(
"plate_3x6"
):
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getPlate3x6
())
case
(
"90T_Tyre"
):
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getTire90T
())
case
(
"beam_0412-140"
):
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0412_140
())
case
(
"beam_0412-92"
):
// func drawModel(_ object_type: String){
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0412_092
())
// switch object_type {
// case ("plate_3x6"):
case
(
"beam_0824-112"
):
// sceneView.scene.rootNode.addChildNode(ar_functions.getPlate3x6())
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0824_096
())
// // case ("90T_Tyre"):
// //sceneView.scene.rootNode.addChildNode(ar_functions.getTire90T())
case
(
"beam_0824-16"
):
//
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0824_016
())
// case ("beam_0412-140"):
case
(
"beam_0824-80"
):
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0412_140())
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0824_080
())
//
case
(
"beam_0824-96"
):
// case ("beam_0412-92"):
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBeam0824_096
())
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0412_092())
case
(
"bracket_3x3"
):
//
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBracket3x3
())
// case ("beam_0824-112"):
case
(
"unnamed_angle"
):
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0824_096())
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getPlate45
())
//
case
(
"unnamed_nut"
):
// case ("beam_0824-16"):
sceneView
.
scene
.
rootNode
.
addChildNode
(
ar_functions
.
getBrassStud
())
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0824_016())
default
:
// case ("beam_0824-80"):
print
(
"Kein Modell für diese Detection vorhanden"
)
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0824_080())
}
// case ("beam_0824-96"):
// sceneView.scene.rootNode.addChildNode(ar_functions.getBeam0824_096())
// case ("bracket_3x3"):
// sceneView.scene.rootNode.addChildNode(ar_functions.getBracket3x3())
// case ("unnamed_angle"):
// sceneView.scene.rootNode.addChildNode(ar_functions.getPlate45())
// case ("unnamed_nut"):
// sceneView.scene.rootNode.addChildNode(ar_functions.getBrassStud())
// default:
// print("Kein Modell für diese Detection vorhanden")
// }
// }
func
createButtons
(){
let
mode_button
=
UIButton
()
mode_button
.
frame
=
CGRect
(
x
:
20
,
y
:
680
,
width
:
200
,
height
:
100
)
mode_button
.
backgroundColor
=
.
white
mode_button
.
setTitle
(
"Change Detection Mode"
,
for
:
.
normal
)
mode_button
.
setTitleColor
(
.
black
,
for
:
.
normal
)
mode_button
.
addTarget
(
self
,
action
:
#selector
(
changeDetectionMode
),
for
:
.
touchUpInside
)
}
let
detect_button
=
UIButton
()
detect_button
.
frame
=
CGRect
(
x
:
240
,
y
:
680
,
width
:
200
,
height
:
100
)
detect_button
.
backgroundColor
=
.
white
detect_button
.
setTitle
(
"Perform Detection"
,
for
:
.
normal
)
detect_button
.
setTitleColor
(
.
black
,
for
:
.
normal
)
detect_button
.
addTarget
(
self
,
action
:
#selector
(
detect
),
for
:
.
touchUpInside
)
let
position_button
=
UIButton
()
position_button
.
frame
=
CGRect
(
x
:
460
,
y
:
680
,
width
:
200
,
height
:
100
)
position_button
.
backgroundColor
=
.
white
position_button
.
setTitle
(
"Select Position"
,
for
:
.
normal
)
position_button
.
setTitleColor
(
.
black
,
for
:
.
normal
)
position_button
.
addTarget
(
self
,
action
:
#selector
(
generateStartPosition
),
for
:
.
touchUpInside
)
position_button
.
tag
=
192
self
.
view
.
addSubview
(
mode_button
)
self
.
view
.
addSubview
(
detect_button
)
self
.
view
.
addSubview
(
position_button
)
}
func
sceneBounds
()
->
(
CGFloat
,
CGFloat
)
{
func
sceneBounds
()
->
(
CGFloat
,
CGFloat
)
{
return
(
self
.
sceneView
.
bounds
.
width
,
self
.
sceneView
.
bounds
.
height
)
return
(
self
.
sceneView
.
bounds
.
width
,
self
.
sceneView
.
bounds
.
height
)
...
@@ -215,20 +261,48 @@ class ViewController: UIViewController, ARSCNViewDelegate {
...
@@ -215,20 +261,48 @@ class ViewController: UIViewController, ARSCNViewDelegate {
if
detectionMode
==
"loop"
{
if
detectionMode
==
"loop"
{
let
dispatch_queue
=
DispatchQueue
.
main
let
dispatch_queue
=
DispatchQueue
.
main
scene_bounds
=
sceneBounds
()
scene_bounds
=
sceneBounds
()
dispatch_queue
.
asyncAfter
(
deadline
:
.
now
()
+
1
,
execute
:
DispatchWorkItem
{
self
.
performDetection
();
self
.
detectionLoop
()})
dispatch_queue
.
asyncAfter
(
deadline
:
.
now
()
+
0.2
,
execute
:
DispatchWorkItem
{
self
.
performDetection
();
self
.
detectionLoop
()})
}
}
else
if
detectionMode
==
"click"
{
else
if
detectionMode
==
"click"
{
performDetection
()
performDetection
()
}
}
}
}
@objc
func
generateStartPosition
(){
while
true
{
if
let
start_pos
=
sceneView
.
session
.
currentFrame
{
start_position_array
.
append
(
start_pos
.
camera
.
transform
)
print
(
"start position bestimmt"
)
self
.
view
.
subviews
.
forEach
({
if
$0
is
UIButton
{
if
$0
.
tag
==
192
{
$0
.
removeFromSuperview
()
}
}
})
break
}
}
}
func
session
(
_
session
:
ARSession
)
{
func
session
(
_
session
:
ARSession
)
{
detectionLoop
()
detectionLoop
()
}
}
override
func
viewDidAppear
(
_
animated
:
Bool
)
{
super
.
viewDidAppear
(
animated
)
}
override
func
viewWillAppear
(
_
animated
:
Bool
)
{
super
.
viewWillAppear
(
animated
)
}
override
func
viewWillDisappear
(
_
animated
:
Bool
)
{
super
.
viewWillDisappear
(
animated
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment